Complete the code to borrow a reference to the variable name.
let name = String::from("Alice"); let name_ref = &[1]; println!("{}", name_ref);
Using &name borrows a reference to the variable name without taking ownership.
Complete the code to create a mutable reference to count.
let mut count = 5; let count_ref = &[1] count; *count_ref += 1; println!("{}", count);
&count which creates an immutable reference.Using &mut count creates a mutable reference allowing modification through the reference.
Fix the error by choosing the correct way to borrow data immutably.
let data = vec![1, 2, 3]; let first = [1] data.get(0).unwrap(); println!("{}", first);
&mut causing mutable borrow error.* incorrectly.Using &data.get(0).unwrap() borrows an immutable reference to the first element.
Fill both blanks to create a mutable reference and modify the value.
let mut value = 10; let val_ref = &[1] value; *val_ref [2] 5; println!("{}", value);
& and trying to modify value.-= or *.Use &mut to borrow mutably and += to add 5 to the value through the reference.
Fill all three blanks to borrow immutably and print the length of the string.
let text = String::from("hello"); let text_ref = &[1]; let length = text_ref.[2](); println!("Length: {}", [3]);
len() on the original variable without borrowing.Borrow text immutably, call len() on the reference, and print the reference variable text_ref length.
