Storing a Variable Amount of Data with Vectors
Instead of turning away people who aren’t on the visitor list, you decide to let them in and store their names for the next party.
Arrays can’t change size. Vectors (Vec) are designed to be dynamically resizable. They can be used like arrays, but you can add to them with a method named push.[29] Vectors can keep growing—you are limited only by the size of your computer’s memory.
Deriving Debug
When you were just storing names as strings, printing them was easy. Send them to println! and you’re done. You want to be able to print the contents of a Visitor structure. The debug placeholders ({:?} —for raw printing—and {:#?}, for “pretty” printing) print any type that supports the Debug trait. Adding ...
Get Hands-on Rust 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.