Variables and Mutability
Level: intro (score: 1)
🚀 These intro exercises are prep for our
Rust Intro Cohort Program.
🧠 Rust makes you think about mutability from the get-go — and that starts with variables.
In Python, you can reassign a variable anytime. In Rust? Hold your horses!
Rust variables are immutable by default, which means if you want to change a value, you need to explicitly make it mutable using mut
.
✅ Your task:
Implement the double_counter
function:
- Create a mutable counter variable and set it to 1
- Double it 5 times in a loop
- Return the final result (should be 32)
This exercise introduces:
let
vslet mut
- basic
for
loops - working with numbers
⚠️ Don’t hardcode the result (we cannot test using ast
like Python) — use a mutable variable and a loop with a range (0..5) to double the counter.
pub fn double_counter() -> i32 {
// your code here
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_double_counter() {
assert_eq!(double_counter(), 32);
}
}