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

# set

A function of the prelude, in scope in every module. Edition 0.2 and later.

```una
pub set[T](edit xs: List[T], index: Int, item: T) -> Unit
  requires 0 <= index and index < xs.len
  ensures xs.len == old(xs).len
```

Replaces the element at `index` of `xs` with `item` (`spec/17 §8`).

## Parameters

- `xs`: the list\, changed in place
- `index`: the position to replace\, from 0
- `item`: the new element

## Returns

`Unit`: nothing\; afterwards `xs` holds `item` at `index`\, and its length is unchanged.

## Requires

- `0 <= index and index < xs.len`: the position is inside the list

Each condition under `requires` is an obligation at every call\, which `una check` proves \(`spec/12 §3`\)\: it reports U5001 when the condition is refuted and U5002 when it is unknown \(`spec/12 §6`\)\, and `una run`\, `una test` and `una build` refuse a package with an obligation that is not proved \(`spec/22 §2`\)\.

## See also

[`append`](https://una-lang.org/en/docs/std/prelude/append/), [`at`](https://una-lang.org/en/docs/std/prelude/at/)

## Cost

Time `O(1) index and set, O(n) copy when the list is shared` (ADR 0107), where:

- `n`: the length of `xs`

## Example

`src/shop.una`

```una
module shop

--- A shelf count corrected after a recount.
pub main() -> Unit
  uses @console: Console[print]
=
  let edit counts = [3, 0, 7]
  if counts.len > 1
    edit counts.set(index: 1, item: 12)
  let total = counts.sum()
  @console.print("total {total}")
```

`una run .` prints:

```text
total 22
```

## Specification

- [`spec/17 §8`](https://una-lang.org/en/docs/spec/17-memory-model/#s8)

Generated from the standard modules of una v0.4.0 ([`docs/std/listing.json`](https://github.com/una-lang/una/blob/5fec93d88932b333feec25e589355a2cb34c37b5/docs/std/listing.json), which is not part of the specification). The specification governs: [`spec/14 §1`](https://una-lang.org/en/docs/spec/14-standard-library/#s1).
