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

# len

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.1 and later.

## Edition 0.1

```una
t.len -> Int
  where t: Text
xs.len -> Int
  where xs: List[T]
```

Built in on `Text` and `List` and, from edition 0.2 on, on `Map` (`spec/14 §2`, `spec/21 §2`). On a `Text` it counts Unicode scalar values (`spec/14 §1`); a map's is never negative (`spec/21 §2`). From edition 0.5 on, a `Stack`, a `Queue`, a `Set`, a `HashMap`, a `HashSet` and a `PriorityQueue` count their items, and a `BitSet` its members, in O(1) (`spec/28 §5`, `§6`, `§7`, `§8`, `§9`).

## Edition 0.2 and later

```una
t.len -> Int
  where t: Text
xs.len -> Int
  where xs: List[T]
m.len -> Int
  where m: Map[K, V]
```

Built in on `Text` and `List` and, from edition 0.2 on, on `Map` (`spec/14 §2`, `spec/21 §2`). On a `Text` it counts Unicode scalar values (`spec/14 §1`); a map's is never negative (`spec/21 §2`). From edition 0.5 on, a `Stack`, a `Queue`, a `Set`, a `HashMap`, a `HashSet` and a `PriorityQueue` count their items, and a `BitSet` its members, in O(1) (`spec/28 §5`, `§6`, `§7`, `§8`, `§9`).

## Edition 0.5 preview

```una
t.len -> Int
  where t: Text
xs.len -> Int
  where xs: List[T]
m.len -> Int
  where m: Map[K, V]
s.len -> Int
  where s: Set[T]
m.len -> Int
  where m: HashMap[K, V]
s.len -> Int
  where s: HashSet[T]
b.len -> Int
  where b: BitSet
```

Built in on `Text` and `List` and, from edition 0.2 on, on `Map` (`spec/14 §2`, `spec/21 §2`). On a `Text` it counts Unicode scalar values (`spec/14 §1`); a map's is never negative (`spec/21 §2`). From edition 0.5 on, a `Stack`, a `Queue`, a `Set`, a `HashMap`, a `HashSet` and a `PriorityQueue` count their items, and a `BitSet` its members, in O(1) (`spec/28 §5`, `§6`, `§7`, `§8`, `§9`).

In editions 0.5. In edition 0.1: no `Map[K, V]` receiver\, since edition 0\.1 has no maps\, and no `Set[T]`\, `HashMap[K, V]`\, `HashSet[T]` or `BitSet` receiver\: it has none of `m.len -> Int`\, `where m: Map[K, V]`\, `s.len -> Int`\, `where s: Set[T]`\, `where m: HashMap[K, V]`\, `where s: HashSet[T]`\, `b.len -> Int` and `where b: BitSet`. In edition 0.2, 0.3, 0.4: no `Set[T]`\, `HashMap[K, V]`\, `HashSet[T]` or `BitSet` receiver\, since `std.collections` begins in edition 0\.5\: it has none of `s.len -> Int`\, `where s: Set[T]`\, `where m: HashMap[K, V]`\, `where s: HashSet[T]`\, `b.len -> Int` and `where b: BitSet`.

## Receiver

- `Text`: a text `t`\: its length in Unicode scalar values
- `List[T]`: a list `xs`\: its number of elements
- `Map[K, V]`: a map `m`\: its number of entries\, never negative
- `Set[T]`: a set `s`\: its number of items
- `HashMap[K, V]`: a hash map `m`\: its number of entries
- `HashSet[T]`: a hash set `s`\: its number of items
- `BitSet`: a bit set `b`\: its number of members\, at most `b.size`

## Returns

`Int`: the length of the receiver\, a text\, a list\, a map\, a set\, a hash table or a bit set.

## See also

[`is_empty`](https://una-lang.org/en/docs/std/prelude/is_empty/), [`bytes`](https://una-lang.org/en/docs/std/prelude/bytes/) (edition 0.2 on)

## Cost

Time `O(1)` on a `Text` (ADR 0070).

## Example

`src/shop.una`

```una
module shop

--- The length of a name, of a cart and of a stock map.
pub main() -> Unit
  uses @console: Console[print]
=
  let name = "Café"
  @console.print("{name.len} characters")
  let cart = ["lamp", "desk"]
  @console.print("{cart.len} items")
  let stock = empty_map().with(key: "lamp", value: 3).with(key: "desk", value: 1)
  @console.print("{stock.len} entries")
```

`una run .` prints:

```text
4 characters
2 items
2 entries
```

## Specification

- [`spec/14 §2`](https://una-lang.org/en/docs/spec/14-standard-library/#s2)
- [`spec/21 §2`](https://una-lang.org/en/docs/spec/21-maps/#s2)
- [`spec/14 §1`](https://una-lang.org/en/docs/spec/14-standard-library/#s1)
- `spec/28 §5`

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