Values and variables
Variables
let gives a value a name:
let answer = 42;
let greeting = "hello";
answer;
42
Variables are immutable by default. To change a variable later, declare it
with mut:
let mut count = 1;
count = count + 1;
count += 10;
count;
12
Assigning to a variable that isn't mut is an error:
[E0219] Error: cannot assign to `count`, which isn't `mut`
mut on a variable only means it can be assigned again. Whether an
array or a struct can be changed is up to
the array or struct itself: see Mutability.
You can declare a new variable with the same name as an old one. The new one shadows the old one from then on:
let x = 1;
let x = x + 1;
x;
2
Type annotations
Bolt infers the type of every variable, so you usually don't write types. When you want to, put the type after the name:
let ratio: f64 = 0.75;
let flag: Bool = True;
ratio;
0.75
Numbers
Bolt has signed integers i8, i16, i32, i64, i128 and isize,
unsigned integers u8 through u128 and usize, and floating-point numbers
f32 and f64.
A number literal gets its type from how it's used. When nothing decides,
an integer is an i32 and a number with a decimal point is an f64. A
suffix gives a literal a type directly:
(1_000_000, 0xff, 10u8, 2.5f32);
(1000000, 255, 10, 2.5)
The usual arithmetic operators work on numbers. Integer division rounds
towards zero, and % is the remainder:
(7 / 2, 7 % 2, 7.0 / 2.0, -(3 * 4));
(3, 1, 3.5, -12)
Both sides of an operator must have the same type: Bolt never converts
numbers for you. Use as to convert explicitly:
(7 as f64 / 2.0, 3.9 as i32);
(3.5, 3)
Booleans
True and False are of type Bool. Compare values with ==, !=, <,
<=, > and >=, and combine booleans with &&, || and !:
(1 < 2, 3 == 4, !(1 < 2) || 2 >= 2);
(True, False, True)
&& and || only evaluate their right side when they need to.
Characters and strings
A character is written between single quotes and has type Char, a
Unicode code point. A string is written between double quotes and has type
String. The prelude defines both. Both understand escapes like \n and
\t:
let c = 'B';
let s = "Bolt\tlang";
(c, s, "con" <> "cat");
('B', "Bolt\tlang", "concat")
<> joins two strings. A Char isn't a number, so as doesn't convert it:
c.to_u32() gives its code point, and Char.from_u32(n) the character with
code point n, if there's one.
Tuples
A tuple groups a fixed number of values, which can have different types.
Its fields are numbered from 0:
let point = (3, "three", 3.0);
(point.0, point.1);
(3, "three")
A tuple can't be changed, unless it's made with mut: after
let t = mut (1, 2);, t.0 = 5; changes its first field. See
mutability for more.
The empty tuple () is called unit. It's the value of statements and of
functions that don't return anything.
Arrays
An array holds any number of values of the same type. Index it with [],
counting from 0:
let primes = [2, 3, 5, 7];
let zeros = [0; 3];
(primes[2], zeros);
(5, [0, 0, 0])
[value; count] makes an array that holds value count times.
Indexing past the end is an error at run time:
[1, 2, 3][5];
[E0301] Error: index 5 is out of bounds for length 3
An array can't be changed, unless it's made with mut, like
mut [0, 0, 0]. Mutability shows how that works.
Comments
// A comment runs to the end of the line.
/* A block comment can span lines,
/* and can be nested. */ */
/// A doc comment describes the item after it.
fn one() -> i32 = 1;
one();
1
Next: functions.