Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print "Hello, world!" in Rust.
Rust
fn main() {
println!([1]);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Forgetting the double quotes around the string.
Using single quotes instead of double quotes.
โ Incorrect
The println! macro requires a string literal inside double quotes to print text.
2fill in blank
mediumComplete the code to declare a variable named x with value 5.
Rust
fn main() {
let [1] = 5;
println!("{}", x);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using a different variable name than the one printed.
Forgetting to declare the variable.
โ Incorrect
The variable name must match the one used in println!, which is x.
3fill in blank
hardFix the error in the code to correctly add two numbers and print the result.
Rust
fn main() {
let a = 3;
let b = 4;
let sum = a [1] b;
println!("{}", sum);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using subtraction or multiplication instead of addition.
Using an invalid operator.
โ Incorrect
The plus sign (+) adds two numbers in Rust.
4fill in blank
hardFill both blanks to create a loop that prints numbers from 1 to 5.
Rust
fn main() {
for i in [1] [2] 6 {
println!("{}", i);
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using 1..=6 which includes 6 and prints 6 as well.
Using 6..1 which is an invalid range.
โ Incorrect
The range 1..6 includes numbers from 1 up to but not including 6, which prints 1 to 5.
5fill in blank
hardFill all three blanks to create a function that returns the square of a number.
Rust
fn square([1]: i32) -> i32 { [2] * [3] } fn main() { let result = square(4); println!("{}", result); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using different variable names inside the function.
Forgetting to return the multiplication result.
โ Incorrect
The function parameter and the multiplication both use the same variable name 'num' to calculate the square.