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

# get

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
m.get(key: K) -> Option[V]
  where m: Map[K, V]
```

`Some` exactly when `m.has(key: k)`. A missing key gives `None`, not a failure (`spec/21 §2`). On a `HashMap`, in O(|k|) expected (`spec/28 §7`).

## Edition 0.5 preview

```una
m.get(key: K) -> Option[V]
  where m: Map[K, V]
m.get(key: K) -> Option[V]
  where m: HashMap[K, V]
```

`Some` exactly when `m.has(key: k)`. A missing key gives `None`, not a failure (`spec/21 §2`). On a `HashMap`, in O(|k|) expected (`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`
- `HashMap[K, V]`: the hash map `m`

## Parameters

- `key`: the key\; from edition 0\.3\, it is taken at the base type of a refined key type `K`\, so a key outside the refinement finds no entry

## Returns

`Option[V]`: `Some` of the value at `key`\, `Some` exactly when `m.has(key: key)`\; a missing key gives `None`\, not a failure.

## See also

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

## Cost

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

- `n`: the number of entries in `m`

## Example

`src/shop.una`

```una
module shop

--- The stock of an item, or a note that it is not stocked.
describe(name: Text, units: Option[Int]) -> Text
=
  when units
    is Some(n) -> "{name}: {n}"
    is None -> "{name}: not stocked"

pub main() -> Unit
  uses @console: Console[print]
=
  let stock = empty_map().with(key: "lamp", value: 3).with(key: "desk", value: 1)
  @console.print(describe("lamp", units: stock.get(key: "lamp")))
  @console.print(describe("chair", units: stock.get(key: "chair")))
```

`una run .` prints:

```text
lamp: 3
chair: not stocked
```

## 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).
