---
title: "set"
url: https://una-lang.org/en/docs/std/builtin/set/
release: v0.4.0
edition: "0.2 and later"
source: https://github.com/una-lang/una/blob/v0.4.0/docs/std/builtins.json
---

# set

A built-in method, resolved by the type of its receiver ([`spec/14 §2`](https://una-lang.org/en/docs/spec/14-standard-library/#s2)). Edition 0.2 and later.

## Edition 0.2 and later

```una
edit m.set(key: K, value: V) -> Unit
  where m: Map[K, V]
```

Updates `m` in place, written `edit m.set(...)`: afterwards `m.has(key: k)` and `m.get(key: k) == Some(v)`, and `m.len` grows by one when the key was not there (`spec/21 §2`). On a `HashMap`, a key already there keeps its place in the listing, in O(|k|) expected, amortized (`spec/28 §7`).

## Edition 0.5 preview

```una
edit m.set(key: K, value: V) -> Unit
  where m: Map[K, V]
edit m.set(key: K, value: V) -> Unit
  where m: HashMap[K, V]
```

Updates `m` in place, written `edit m.set(...)`: afterwards `m.has(key: k)` and `m.get(key: k) == Some(v)`, and `m.len` grows by one when the key was not there (`spec/21 §2`). On a `HashMap`, a key already there keeps its place in the listing, in O(|k|) expected, amortized (`spec/28 §7`).

In editions 0.5. In edition 0.2, 0.3, 0.4: no `HashMap[K, V]` receiver\, since `std.collections` begins in edition 0\.5\: it has no `where m: HashMap[K, V]`.

## Receiver

- `Map[K, V]`: the map `m`\, changed in place\, written `edit m.set(...)`
- `HashMap[K, V]`: the hash map `m`\, changed in place\, written `edit m.set(...)`\; a key already there keeps its place in the listing

## Parameters

- `key`: the key\; from edition 0\.3\, with a refined key type `K`\, it is stored at `K`\, an obligation that `una check` proves at the call
- `value`: the new value at `key`\; from edition 0\.3\, with a refined value type `V`\, it is stored at `V`\, an obligation that `una check` proves at the call

## Returns

`Unit`: nothing\; afterwards `m.get(key: key) == Some(value)`\, and `m.len` grows by one when `key` was not there.

## Use instead

- [`with`](https://una-lang.org/en/docs/std/builtin/with/) for a new map\, leaving the old one as it is

## See also

[`remove`](https://una-lang.org/en/docs/std/builtin/remove/)

## Cost

Time `O(log n) per lookup, insertion and removal` on a `Map[K, V]` (spec/21 §5), where:

- `n`: the number of entries in `m`\; in the native runtime the update is in place when `m` is uniquely held\, and on a copy\, in O\(n\)\, otherwise

## Example

`src/shop.una`

```una
module shop

--- A recount replaces the stock of an item.
pub main() -> Unit
  uses @console: Console[print]
=
  let edit stock = empty_map().with(key: "lamp", value: 3)
  edit stock.set(key: "lamp", value: 5)
  let units = stock.get(key: "lamp").or_else(default: 0)
  @console.print("lamp: {units}, entries: {stock.len}")
```

`una run .` prints:

```text
lamp: 5, entries: 1
```

## Specification

- [`spec/21 §2`](https://una-lang.org/en/docs/spec/21-maps/#s2)
- `spec/28 §7`

Generated from una v0.4.0 ([`docs/std/builtins.json`](https://github.com/una-lang/una/blob/5fec93d88932b333feec25e589355a2cb34c37b5/docs/std/builtins.json), which is not part of the specification): built-in methods have no declaration, so una writes these signature lines and tests each against its checker. The specification governs: [`spec/14 §2`](https://una-lang.org/en/docs/spec/14-standard-library/#s2).
