Complete the code to add two numbers and print the result.
fn main() {
let sum = 5 [1] 3;
println!("{}", sum);
}The + operator adds two numbers together.
Complete the code to multiply two numbers and print the result.
fn main() {
let product = 4 [1] 7;
println!("{}", product);
}The * operator multiplies two numbers.
Fix the error in the code to correctly divide two numbers and print the result as a float.
fn main() {
let result = 10.0 [1] 4.0;
println!("{}", result);
}The / operator divides one number by another. Using floats ensures decimal division.
Fill in the blank to create a dictionary with word lengths for words longer than 3 characters.
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);
}The > operator filters words longer than 3 characters.
Fill all three blanks to create a map of uppercase words to their lengths for words longer than 3 characters.
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);
}We filter words longer than 3 with >, convert words to uppercase with to_uppercase(), and get their length with len().
