beginnerBasics
Primitive Types
Scalar and compound types in Rust: integers, floats, booleans, tuples, arrays.
Primitive Types
Scalar and compound types in Rust: integers, floats, booleans, tuples, arrays.
Difficulty
Beginner
Code
rust
fn main() {
let x: i32 = 42;
let y: f64 = 3.14;
let active: bool = true;
let letter: char = 'R';
let tup: (i32, f64, char) = (1, 2.5, 'a');
let (a, b, c) = tup;
println!("{} {} {}", a, b, c);
let arr: [i32; 3] = [10, 20, 30];
println!("first: {}", arr[0]);
}Explanation
This example demonstrates how to use primitive types in Rust. Read the code carefully to understand the flow. Pay attention to where values are created, borrowed, moved, or consumed.
Key Concepts
- Rust's strong type system catches errors at compile time
- Ownership and borrowing rules ensure memory safety
- Pattern matching makes code expressive and exhaustive
Related Topics
Browse more examples in the basics category to build a complete understanding of this topic.