Crate var[stability] [-] [+] [src]

A macro to declare multiple mutable variables in one statement.

var! { ... } is a generalised form of let mut x = ...;, allowing for several mutable variables to be declared and initialised at once, inspired by the keyword of the same name in languages like Nim and C#.

Available on crates.io. Source.

Grammar

"var! {"
    (
        identifier (":" type)? "=" expression
    )*
"}"

where

Notably,

Examples

#[macro_use] extern crate var;

var! {
    a = 1,
    b: &str = "foo",
    c = 3.0,
}

a += 1;
b = "bar";
c *= 7.0;

Generating Fibonacci numbers with a loop:

#[macro_use] extern crate var;

fn fibonacci(n: u32) -> u64 {
    var! {
        a: u64 = 0,
        b = 1,
    }
    for _ in 0..n {
        let tmp = a + b;
        a = b;
        b = tmp;
    }
    return a
}

fn main() {
    assert_eq!(fibonacci(10), 55);
}

Macros

__var_internals!

Implementation details. Do not use this directly.

var!

The main macro, see crate docs for details.