Drag and drop the correct keywords and symbols to complete the Rust code snippets!
Q1: Define the main function.
() {
// Code goes here
}
Q2: Print "Hello, Rust!" to the console.
()
Q3: Declare an immutable variable `x` with value `10`.
x 10
Q4: Declare a mutable variable `y` with value `20`.
y 20
Q5: Add a single-line comment.
This is a comment
Q6: Complete the `if` statement.
x 5 {
// do something
}
Q7: Add an `else` block.
if true { } {
// otherwise
}
Q8: Start an infinite loop.
{
// repeat forever
}
Q9: Exit a loop early.
loop { ;
}
Q10: Loop through numbers 1 to 5.
i 16 {
// use i
}
Q11: Start a pattern match.
some_value {
_ => {}
}
Q12: Define a struct named `Point`.
Point {
x: i32,
y: i32,
}
Q13: Define an enum named `Color`.
Color {
Red,
Green,
Blue,
}
Q14: Explicitly return a value.
fn get_five() -> i32 {
5
}
Q15: Annotate variable `age` as `u32`.
let age = 30
Q16: Declare a module named `utils`.
utils
Q17: Bring `HashMap` into scope.
stdcollectionsHashMap
Q18: Pass a reference to `val`.
fn print_ref(s: String) { }
let val = String::from("Rust");
print_ref(val)
Q19: Dereference a pointer `ptr`.
let num = ptr
Q20: Define a function with a generic type `T`.
fn identity<>(x: ) -> { x }