Complete the code to create a vector and access its first element.
let numbers = vec![10, 20, 30]; let first = numbers[1]0; println!("{}", first);
Vectors in Rust use square brackets [] to access elements by index.
Complete the code to insert a key-value pair into a HashMap.
use std::collections::HashMap; let mut map = HashMap::new(); map.[1]("apple", 3); println!("{:?}", map);
push which is for vectors, not HashMaps.The insert method adds a key-value pair to a HashMap in Rust.
Fix the error in the linked list traversal to print all values.
struct Node {
value: i32,
next: Option<Box<Node>>,
};
fn print_list(mut current: &Option<Box<Node>>) {
while let Some(node) = current.as_ref() {
println!("{}", node.value);
current = &node.[1];
}
}value or a non-existent prev field.To move to the next node in a linked list, access the next field.
Fill both blanks to create a HashMap from a vector of keys and assign values.
use std::collections::HashMap; let keys = vec!["a", "b", "c"]; let mut map = HashMap::new(); for key in keys { map.[1](key, key.[2]()); }
push instead of insert, or clone instead of to_string().Use insert to add key-value pairs, and to_string() to convert &str to String.
Fill all three blanks to filter an array and collect values into a vector.
let numbers = [1, 2, 3, 4, 5]; let evens: Vec<i32> = numbers.iter() .[1](|&x| x [2] 2 == 0) .[3](|&x| x) .collect(); println!("{:?}", evens);
copied instead of map, or wrong operator for modulo.Use filter to select even numbers, % for modulo, and map to transform references.
