Scalar types and casts in Rust
Rust's sized integers, floats, bools, and chars.
fn main() {
let byte: u8 = 255;
let big: i64 = 9_000_000_000;
let pi = 3.14159_f64;
let yes = true;
let letter = 'R';
let widened = byte as i64 + big;
let truncated = pi as i32;
let flag = yes as u8;
println!("{widened} {truncated} {flag} {letter}");
}
How it works
- Types like
u8andi64state width and sign. ascasts between numeric types explicitly.- A
charis a full Unicode scalar.
Keywords and builtins used here
asf64fni32i64letmainu8
The run, in numbers
- Lines
- 12
- Characters to type
- 253
- Tokens
- 66
- Three-star pace
- 80 tpm
At the three-star pace of 80 tokens a minute, this run takes about 50 seconds.
Step 3 of 6 in Variables & types, step 3 of 39 in Language basics.