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

# sum

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

## Edition 0.1

```una
pub sum(xs: List[Int]) -> Int
```

The exact sum of the elements as an `Int`, `0` when `xs` is empty; `Int` has no bound, so it never overflows. In edition 0.2 and later it takes any integer type and adds the values as `Int` (`spec/14 §1`, `spec/20 §6`).

## Edition 0.2 and later

```una
pub sum[T: Integer](xs: List[T]) -> Int
```

The exact sum of the elements as an `Int`, `0` when `xs` is empty; `Int` has no bound, so it never overflows. In edition 0.2 and later it takes any integer type and adds the values as `Int` (`spec/14 §1`, `spec/20 §6`).

In editions 0.2, 0.3, 0.4, 0.5. In edition 0.1: `sum(xs: List[Int]) -> Int`\: `Int` elements only.

## Parameters

- `xs`: the list

## Returns

`Int`: the exact sum of the elements\, as an `Int`\; `0` for an empty list\; it never overflows.

## See also

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

## Example

`src/shop.una`

```una
module shop

--- The total of a cart, in cents.
pub main() -> Unit
  uses @console: Console[print]
=
  let total = [1999, 499].sum()
  @console.print("total {total}")
```

`una run .` prints:

```text
total 2498
```

## Specification

- [`spec/14 §1`](https://una-lang.org/en/docs/spec/14-standard-library/#s1)
- [`spec/20 §6`](https://una-lang.org/en/docs/spec/20-constraints-on-generics/#s6)

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