Puzzle 17How Long Is a Vector?
 fn​ ​main​() {
 let​ ​mut​ my_vec = ​Vec​::​with_capacity​(1);
  my_vec​.push​(​"Hello"​);
  println!(​"{}"​, my_vec​.capacity​());
  my_vec​.push​(​"World"​);
  println!(​"{}"​, my_vec​.capacity​());
 }

Guess the Output

images/aside-icons/important.png

Try to guess what the output is before moving to the next page.

images/hline.png

The program will display the following output:

 1
 4
images/hline.png

Discussion

Vectors contain two things: a length indicating ...

Get Rust Brain Teasers now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.