Back to questions
JuniorTheoryCommon
What does the program print?
fn main() {
let mut v = Vec::with_capacity(10);
v.push(1);
println!("{} {}", v.len(), v.capacity());
}1 10
- Expecting
with_capacity(10)to create ten elements: it only reserves room, so after onepushthe length is 1. - Expecting
capacityto followlen: it counts the slots in the buffer, which stay 10 until the vector outgrows them.
- What would
lenandcapacitybe after eleven calls topush? - What does
v[0]do right afterVec::with_capacity(10), before anypush?