Tuples

Tuples in Rune are fixed-size sequences of values. Similarly to a vector, tuples can contain any sequence of values. But there's no way to change the size of a tuple.

pub fn main() {
    let values = ("Now", "You", "See", "Me");
    dbg(values);

    values.2 = "Don't";
    values.3 = "!";
    dbg(values);
}
$> cargo run --bin rune -- run scripts/book/tuples/tuple_masquerade.rn
("Now", "You", "See", "Me")
("Now", "You", "Don\'t", "!")
== () (38.3136ms)

The following is a simple example of a function returning a tuple:

fn foo() {
    (1, "test")
}

pub fn main() {
    dbg(foo());
}
$> cargo run --bin rune -- run scripts/book/tuples/basic_tuples.rn
(1, "test")
== () (387.6┬Ás)

Tuples can also be pattern matched:

pub fn main() {
    match ("test", 1) {
        ("test", n) => {
            dbg("the first part was a number:", n);
        }
        _ => {
            dbg("matched something we did not understand");
        }
    }
}
$> cargo run --bin rune -- run scripts/book/tuples/tuple_patterns.rn
"the first part was a number:"
1
== () (7.7892ms)

Using tuples from Rust

Tuples are represented externally as primitive tuple types.

use rune_tests::run;
use std::sync::Arc;

fn main() -> runestick::Result<()> {
    let context = Arc::new(rune_modules::default_context()?);

    let object: (i64, i64) = run(
        &context,
        r#"
        pub fn calc(input) {
            (input.0 + 1, input.1 + 2)
        }
        "#,
        &["calc"],
        ((1, 2),),
    )?;

    println!("{:?}", object);
    Ok(())
}
$> cargo run --example tuple
(2, 4)