Rust Lesson 2: Variables, Mutability and Types

📎 This article includes 1 downloadable practice file ↓

⏱ 1 min read

📘 Rust Beginner Course · Lesson 2 of 6

“`rust
let rate: f64 = 60.0; // immutable
let mut passed = 0; // mutable
for m in [78, 91, 45, 33] { if m >= 40 { passed += 1; } }
let amount = 12_u32 as f64 * rate;
println!(“{amount:.2}”);
“`

Immutability by default prevents accidental changes. Types: i32/u32 integers, f64 decimals, bool, &str (borrowed text) and String (owned, growable text).

💡 Let the compiler teach you: Rust’s error messages usually say exactly what to change.

Code for this course follows standard, current syntax; we test JavaScript and PowerShell scripts before publishing, and R/Java/Rust examples are kept to core language features. Tell us if anything fails on your setup.

Practice

Change a variable without mut and read the compiler message.

📎 Practice files for this article

Free to use for learning. Files with macros (.bas) are plain text — import them with Alt+F11 → File → Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free · AI can be wrong

Leave a Reply

Your email address will not be published. Required fields are marked *