Rust basics
Quiz
What you learned
Rust basics quiz. 12 questions. You need 80% to pass.
What does this print?
fn main() {
let name = 5;
{
let name = name + 1;
println!("inner {name}");
}
println!("outer {name}");
}let wide: i32 = 300. What are these two values?
let truncated = wide as u8;
let too_big: Result<u8, _> = wide.try_into();What does this print?
fn main() {
let x: i32 = 7 / 2;
println!("{x}");
}This function does not compile. Why?
fn area(width: i32, height: i32) -> i32 {
width * height;
}Which loop prints 5, then 4, then 3, then 2, then 1?
for n in 5..1 {}
for n in (1..=5).rev() {}The loops print 0,0 then 0,1 then 0,2 then 1,0 and stop. Which break does that?
'outer: for x in 0..3 {
for y in 0..3 {
if x == 1 && y == 1 {
break 'outer;
}
println!("{x},{y}");
}
}What happens with this constant index?
fn main() {
let numbers = [10, 20, 30, 40, 50];
println!("{}", numbers[10]);
}v has 3 elements. What is v.get(9)?
You insert Grace, then Ada, and print every pair. Which map prints Ada before Grace every time?
What does this print?
fn main() {
let text = "héllo";
println!("{} {}", text.len(), text.chars().count());
}A match on an i32 has arms for 1 and 2 and no _. A PHP match has the same two arms and no default. The value is 3. What happens?
age is None. What does this do?
let Some(years) = age else {
return;
};
println!("{years}");Will this compile?
fn main() {
let mut n = 1;
let r = &n;
n += 1;
println!("{r}");
}What does this print?
fn main() {
let v = vec![1, 2, 3];
let n: usize = v.iter().map(|x| x * 2).filter(|x| *x > 2).count();
println!("{n}");
}