I have a reference to a Vec and I want to extract a reference to exactly one element from it, and panic if there are any more elements or zero. The equivalent if I had an array would be:
let [x] = list;
but Vecs are dynamically sized, so that won't work here. I can think of one way to do it with a reference to a Vec, and a couple more that require ownership, but I'm wondering if there's a shorter and simpler way.
Unowned Option 1: Use assert! and indexing
assert_eq!(list.len(), 1);
let x = &list[0];
Owned Option 1: Use try_into()
let [x]: [i32; 1] = list.try_into().unwrap();
Owned Option 2: Use assert! and pop
assert_eq!(list.len(), 1);
let x = list.pop();
So, is there a shorter and clearer way?