Complete the code to evaluate the sum of two numbers and print the result.
fn main() {
let a = 5;
let b = 3;
let result = a [1] b;
println!("Result: {}", result);
}- instead of addition.* or division / by mistake.The + operator adds two numbers. Here, a + b calculates the sum.
Complete the code to calculate the remainder when 10 is divided by 3.
fn main() {
let x = 10;
let y = 3;
let remainder = x [1] y;
println!("Remainder: {}", remainder);
}/ instead of remainder %.The % operator gives the remainder of division. Here, 10 % 3 equals 1.
Fix the error in the expression to correctly calculate the power of 2 raised to 3.
fn main() {
let base = 2;
let exponent = 3;
let power = base.[1](exponent);
println!("Power: {}", power);
}^ or ** which are not power operators in Rust.Rust uses the pow method to calculate powers. The correct syntax is base.pow(exponent).
Fill in the blank to create a HashMap that maps words to their lengths only if length is greater than 3.
fn main() {
let words = vec!["apple", "cat", "banana", "dog"];
let lengths = words.iter().filter(|&&word| word.len() [1] 3).map(|&word| (word, word.len())).collect::<std::collections::HashMap<_, _>>();
println!("{:?}", lengths);
}< or == which filter wrong words.!= which does not check length properly.The filter keeps words with length greater than 3 using word.len() > 3.
Fill in the two blanks to create a HashMap from words to their uppercase forms only if the word length is less than 6.
fn main() {
let words = vec!["apple", "cat", "banana", "dog"];
let map = words.iter()
.filter(|&&word| word.len() [1] 6)
.map(|&word| (word, word.[2]()))
.collect::<std::collections::HashMap<_, _>>();
println!("{:?}", map);
}> instead of < in the filter.to_lowercase() instead of to_uppercase().The filter uses < to keep words shorter than 6. The method to_uppercase() converts words to uppercase.