cairo by example


Span is a struct that represents a snapshot of an Array. All methods provided by Array can also be used with Span, with the exception of the append() method.

To create a Span of an Array, simply call the span() method.

Modifying the example from the snapshots example

use array::ArrayTrait;
use array::SpanTrait;

// Receives a Span
fn sum_starting_two(data: Span<u32>) -> u32 {
    // data.append(5_u32); <- this fails
    *data[0] + *data[1]

fn main() -> u32 {
    let mut data: Array<u32> = ArrayTrait::new();
    sum_starting_two(data.span()) // Using a Span

Try it out!
  1. Install the toolchain:
    • For macOS and Linux, run our script:
    • curl -sL | bash -s 2.2.0
    • For Windows and others, please see the official guide
  2. Run the example:
    1. Copy the example into a spans.cairo file and run with:
    2. %!s(<nil>) spans.cairo

prev (operator overloading) next (contracts)