Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add two numbers and print the result.
Rust
fn main() {
let sum = 5 [1] 3;
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.
โ Incorrect
The + operator adds two numbers together.
2fill in blank
mediumComplete the code to multiply two numbers and print the result.
Rust
fn main() {
let product = 4 [1] 7;
println!("{}", product);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using addition or division instead of multiplication.
โ Incorrect
The * operator multiplies two numbers.
3fill in blank
hardFix the error in the code to correctly divide two numbers and print the result as a float.
Rust
fn main() {
let result = 10.0 [1] 4.0;
println!("{}", result);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using integer division or wrong operator causing errors.
โ Incorrect
The / operator divides one number by another. Using floats ensures decimal division.
4fill in blank
hardFill in the blank to create a dictionary with word lengths for words longer than 3 characters.
Rust
fn main() {
let words = ["apple", "cat", "banana", "dog"];
let lengths: std::collections::HashMap<_, _> = words.iter()
.filter(|&&word| word.len() [1] 3)
.map(|&word| (word, word.len()))
.collect();
println!("{:?}", lengths);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using < or == which would filter wrong words.
โ Incorrect
The > operator filters words longer than 3 characters.
5fill in blank
hardFill all three blanks to create a map of uppercase words to their lengths for words longer than 3 characters.
Rust
fn main() {
let words = ["apple", "cat", "banana", "dog"];
let lengths: std::collections::HashMap<_, _> = words.iter()
.filter(|&&word| word.len() [1] 3)
.map(|&word| (word.[2](), word.[3]()))
.collect();
println!("{:?}", lengths);
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
๐ก Hint
Common Mistakes
Mixing up length and case conversion methods.
โ Incorrect
We filter words longer than 3 with >, convert words to uppercase with to_uppercase(), and get their length with len().