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

# slice

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

```una
pub slice(t: Text, start: Int, end: Int) -> Text
  requires 0 <= start and start <= end and end <= t.len
  ensures result.len == end - start
```

The scalar values of `t` from position `start` up to, not including, `end` (`spec/22 §2`).

## Parameters

- `t`: the text
- `start`: the scalar position\, from 0\, of the first scalar value to take
- `end`: the scalar position after the last scalar value to take

## Returns

`Text`: the scalar values of `t` from `start` up to\, not including\, `end`\; its length is `end - start`.

## Requires

- `0 <= start and start <= end and end <= t.len`: the positions are in order and inside `t`

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

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

## Cost

Time `O(result), after the index is built once` (ADR 0070), where:

- `result`: the length of the result in UTF\-8 bytes
- index: on a text that is not all ASCII\, the byte offset of every 64th scalar value\, its checkpoints\: built in O\(b\) by the first call that needs a position and kept with the value\, so at most once per value\; an ASCII text needs none \(spec\/22 §6\)

## Example

`src/shop.una`

```una
module shop

--- The range of an SKU, the part before its dash. `find` gives a position that `slice` is
--- proved to accept.
range_of(sku: Text) -> Text
=
  when sku.find(text: "-")
    is Some(at) -> sku.slice(start: 0, end: at)
    is None -> sku

pub main() -> Unit
  uses @console: Console[print]
=
  @console.print(range_of("LAMP-01"))
  @console.print(range_of("CAFÉ-01"))
  @console.print(range_of("GIFTCARD"))
```

`una run .` prints:

```text
LAMP
CAFÉ
GIFTCARD
```

## Specification

- [`spec/22 §2`](https://una-lang.org/en/docs/spec/22-text/#s2)

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