Activity 18 - Poll Questions

You'll need to re-submit once for each question as we go!
Poll Question 1
Which of these will compile?
#![allow(unused)] fn main() { // Option A let mut data = vec![1, 2, 3]; let r1 = &data; let r2 = &data; println!("{:?} {:?}", r1, r2); // Option B let mut data = vec![1, 2, 3]; let r1 = &data; let r2 = &mut data; println!("{:?} {:?}", r1, r2); // Option C let mut data = vec![1, 2, 3]; let r1 = &mut data; let r2 = &mut data; println!("{:?} {:?}", r1, r2); }
A) Only A compiles B) A and B compile C) All three compile D) None compile
Poll Question 2
What happens when you run this code?
#![allow(unused)] fn main() { let emoji = "🦀"; let slice = &emoji[0..2]; println!("{}", slice); }
A) Prints "🦀" B) Prints nothing (empty string) C) Compiler error D) Runtime panic
Poll Question 3: Function Design - Style Preference
Which approach do you prefer for getting the first 3 characters of a string?
#![allow(unused)] fn main() { // Option A: Mutable borrow - modifies in place fn keep_first_three(s: &mut String) { *s = s.chars().take(3).collect(); } // Option B: Immutable borrow - returns new String fn first_three(s: &str) -> String { s.chars().take(3).collect() } // Usage A: let mut text = String::from("Hello World"); keep_first_three(&mut text); println!("{}", text); // "Hel" // Usage B: let text = String::from("Hello World"); let first = first_three(&text); println!("{}", first); // "Hel" }
A) Option A B) Option B C) Depends on context
Poll Question 4
What's the output?
fn main() { let mut data = vec![1, 2, 3, 4, 5]; let slice = &data[1..3]; data.push(6); println!("{:?}", slice); }
A) Prints [2, 3] B) Prints [2, 3, 6] C) Compiler error D) Runtime panic
Poll Question 5: String Type Choice
You're writing a function that finds the first word in text. Which signature is best?
#![allow(unused)] fn main() { // Option A fn first_word(text: String) -> String { ... } // Option B fn first_word(text: &String) -> &str { ... } // Option C fn first_word(text: &str) -> String { ... } // Option D fn first_word(text: &str) -> &str { ... } }
A) Option A B) Option B C) Option C D) Option D