Basics and Types
Two topics, learned together: giving names to values, and numbers that mean exactly what they say. Nearly every beginner surprise lives in this chapter, so read it even if the rest of the guide feels familiar.
Naming values with let and const
let declares a binding you can reassign; const declares one you cannot. Names must be declared before use. There are no implicit globals, so a typo fails fast instead of becoming mystery state:
let score = 0;
score = score + 10;
const label = "score:";
println!(label, score);That prints score: 10. Type annotations are optional on bindings but always allowed. Put them where they document intent:
const maxRetries: Int = 3;
let attempt = 0;
while attempt < maxRetries {
attempt = attempt + 1;
}
println!("tried", attempt, "times");Common mistake: reassigning a const, or assigning a different type to a let. Bindings keep the type they are born with: let a = 2; a = "hello"; does not compile. When you see a type error on assignment, check the first line, not the failing one.
One integer type: Int
Int is always a signed 64-bit whole number on every platform. There is no platform-dependent int, no long that changes size between operating systems, and no u8/i32 family to juggle for everyday counting. The range is roughly ±9.2e18. If you outgrow that, you will know.
Arithmetic wraps on overflow with two's-complement semantics. Division or remainder by zero is a runtime fatal. You get an abort, never undefined behavior or a quietly wrong answer:
println!(41 + 1);
println!(7 / 2);
println!(7 % 3);That prints 42, 3 (integer division truncates), and 1. Convert a float with Int(x), which truncates toward zero, and render a number with toString():
println!(Int(2.9));
println!(Int(-2.9));Both print as you would expect: 2 and -2.
Two float types, on purpose
Float is an IEEE-754 double, the strict, predictable default. FastFloat is the same storage with relaxed optimization rules for hot numeric code. You opt in explicitly, and the two never mix in one expression:
let c: Float = 10.5 * 2.0 + 20.25;
println!(c);
let scaled = c.asFast() * 2.0.asFast();
println!(scaled == 82.5.asFast(), scaled.asStrict());asFast() moves to relaxed mode, asStrict() comes back. If the compiler complains about mixing Float and FastFloat (error E305), name the conversion you mean instead of guessing. That explicitness is the whole point.
Handy extras on Float: Float.nan(), Float.isNaN(x), Float.fma(a, b, c) for a fused multiply-add, and toBits() / Float.fromBits(bits) for bit-level round trips.
Strings and interpolation
String holds UTF-8 text. ${...} inside a string evaluates an expression inline, and + concatenates. {...} interpolates the same way. The two forms are interchangeable, so pick one per file and stay with it. A literal brace is \{:
let name = "ore";
let level = 7;
println!("player ${name} reached level ${level + 1}");
println!("sum {level + 1}");
println!("ab" + "cd");+ with a String on either side concatenates, converting the other operand: "5" + 1 is "51", not 6. Useful measurements on strings: length() (characters, not bytes), slice(start, end), indexOf(needle), trim(), and charCodeAt(index). The full toolkit (contains, split, replace, and friends) lives in Collections. For now, interpolation plus print covers most programs.
Truth values
Bool has exactly two values, true and false, and conditions demand it. Comparison operators (==, !=, <, <=, >, >=) produce Bool, and &&, ||, ! combine them:
let hp = 30;
let shielded = false;
if hp > 0 && !shielded {
println!("vulnerable");
}Common mistake: writing if 1 { } or if name { } out of habit from C or Python. rnx conditions take a Bool and nothing else. Say what you mean (if hp > 0, if name != "").
Operators at a glance
| Group | Operators |
|---|---|
| Arithmetic | + - * / % (with unary -) |
| Comparison | == != < <= > >= |
| Boolean | && || ! |
| Bitwise | & | ^ ~ |
| Null-safe | ?? ?. postfix ? (see Error Handling) |
| Ternary | cond ? a : b (see Control Flow) |
Next: Control Flow: branches, loops, pattern matching with switch, and defer.