Back to questions
JuniorTheoryCommon
What does the program print?
fn main() {
let v = vec![1, 2, 3];
println!("{:?} {:?}", v.get(1), v.get(10));
}Some(2) None
- Expecting a panic, as with
v[10]:getchecks the index itself and returnsNonewhen there is no element. - Expecting
get(1)to return the first element: indices start at 0, soget(1)isSome(2).
- What would
println!("{}", v[10])do in the same program? - How would you print the element from
v.get(1)without theSomearound it?