rust / beginner
Snippet
Primitive Data Types and Type Inference
Rust has several primitive scalar types: integers (i32, i64), floating-point numbers (f64), characters (char), and booleans (bool). The compiler can infer types in many cases, but you can explicitly annotate them with a colon and type name.
snippet.rs
1
2
3
4
5
6
7
8
9
10
11
fn main() {let integer: i32 = 42;let float = 3.14; // f64 by defaultlet character = 'R';let is_active = true;println!("Integer: {integer}");println!("Float: {float}");println!("Char: {character}");println!("Boolean: {is_active}");}
Breakdown
1
let integer: i32 = 42;
Signed 32-bit integer with explicit type annotation
2
let float = 3.14;
Floating-point number, compiler infers f64 type by default
3
let character = 'R';
Single Unicode character enclosed in single quotes
4
let is_active = true;
Boolean value, either true or false
5
println!("Integer: {integer}");
Prints integer using string interpolation