Skip to main content

Variables

src/variables.duck
fn main() {
// const vars are immutable
const x = 10;
// let vars are mutable
let y = 10;

// immutable references don't allow mutation of the thing pointed to
const immutable_pointer_to_x = &x;
const immutable_pointer_to_y = &y;

// mutable references allow mutation of the thing pointed to
const mutable_pointer_to_y = &mut y;
// cannot take mutable references of const vars
// const mutable_pointer_to_x = &mut x; <- error

// f is a lambda that is allowed to make modifications to it's environment (e.g. mutating y)
const f = mut fn() {
// mutating const vars is still not allowed
// x = 10; <- error

// mutating y is allowed because it is a let var
y = 20;
};

// f2 is a lambda that is NOT allowed to make modifications to it's environment
const f2 = fn() {
// even tho y is a mutable variable, f2 is not allowed to make modifications
// y = 20; <- error
};
// caling f now changes y to 20
f();
// caling f2 won't change anything
f2();
}