Skip to main content

Variables & References

Variables hold values while a function runs. They aren't part of the document: when the function ends, they're gone.

let and const​

let declares a variable you can change. const declares one you can't reassign. Either can have a type, which casts what's assigned to it:

declare.stofPlayground ↗
price: 20

#[main]
fn main() {
  let total = self.price;
  total += 5;

  const label: str = 42;      // cast to '42'
  pln(`${label}: ${total}`);
}
Output

const stops reassignment, not change: const list = []; list.push_back(1); is fine.

Scopes​

A block ({ ... }) has its own scope. Code inside it can use the variables outside it, and the variables declared inside it end with the block:

scopes.stofPlayground ↗
#[main]
fn main() {
  let outer = 10;
  {
      let inner = 5;
      pln(outer + inner);
  }
  pln(outer);
}
Output

Functions are separate: a function doesn't see the variables of the function that called it. See Functions.

Destructuring​

Destructuring takes a value apart into variables in one step. From an object (or a map with string keys), name the fields you want:

destructure-object.stofPlayground ↗
user: {
  name: 'Ada'
  age: 36
  address: { city: 'London' }
}

#[main]
fn main() {
  const { name, age: years, role = 'member', address: { city } } = self.user;

  pln(name);       // a field, into a variable of the same name
  pln(years);      // age, renamed
  pln(role);       // not there, so the default
  pln(city);       // nested
}
Output

From a list or tuple, variables take the items in order, and _ skips one:

destructure-list.stofPlayground ↗
#[main]
fn main() {
  const [first, _, third] = ['a', 'b', 'c'];
  pln(first + third);

  const (key, value) = ('plan', 'pro');
  pln(`${key} = ${value}`);
}
Output

It works in loops too, which is handy for maps and lists of objects:

destructure-loop.stofPlayground ↗
users: [
  { name: 'Ada', plan: 'pro' },
  { name: 'Grace', plan: 'free' },
]

#[main]
fn main() {
  for (const { name, plan } in self.users) {
      pln(`${name}: ${plan}`);
  }

  const limits = map(('calls', 100), ('seats', 5));
  for (const (key, value) in limits) {
      pln(`${key} = ${value}`);
  }
}
Output

A missing field or item gives null, or its default if it has one. Destructuring something of the wrong kind, like null, throws an error. Use let instead of const to change the variables afterward.

Copied or Shared​

Simple values (numbers, strings, booleans, versions) are copied when you put them in a variable. Changing the variable doesn't change where it came from:

copied.stofPlayground ↗
count: 1

#[main]
fn main() {
  let n = self.count;
  n += 10;
  pln(self.count);    // still 1
}
Output

Lists, maps, sets, and objects are shared instead. A variable holding a list is the same list, so changing it changes the original. Use copy(..) when you want a separate one:

shared.stofPlayground ↗
tags: ['a']

#[main]
fn main() {
  let same = self.tags;
  same.push_back('b');
  pln(self.tags);     // ['a', 'b']: same list

  let separate = copy(self.tags);
  separate.push_back('c');
  pln(self.tags);     // unchanged
}
Output

References with &​

& makes a simple value shared too: the variable refers to the original instead of a copy:

reference.stofPlayground ↗
count: 1

#[main]
fn main() {
  let n = &self.count;
  n += 10;
  pln(self.count);    // 11
}
Output

The same works for arguments, so a function can change a variable it's given:

by-reference.stofPlayground ↗
fn add_name(greeting: str, name: str) {
  greeting.push(name);
}

#[main]
fn main() {
  let s = 'Hello, ';
  self.add_name(&s, 'Ada');
  pln(s);
}
Output

And for list items, including in loops:

list-references.stofPlayground ↗
#[main]
fn main() {
  const prices = [10, 20, 30];

  let first = &prices[0];
  first = 15;

  for (let price in &prices) price *= 2;
  pln(prices);
}
Output

Next, Control Flow.