11 Rust Crash Course: Collections

preview_player
Показать описание
Rust includes a variety of built-in collection types that have different performance tradeoffs and usage scenarios. The most ubiquitous is Vec, a resizable array type that supports fast push/pop from one end but slower index-based access. Like all Rust collections, Vec handles memory management automatically thanks to Rust's ownership system. Strings are often stored as Vec internally. For key-value data, HashMap provides fast lookup time through hashing but without ordering guarantees.

When order matters, BTreeMap ensures keys remain sorted while allowing log(n) range queries and iteration. Similarly, HashSet offers fast set operations without ordering while BTreeSet preserves ordering. Linked list types like LinkedList offer efficient prepends and appends but slow random access. Fixed sized arrays [T; LEN] deliver fast access without allocation but lose flexibility due to their compile time size.

A major strength of Rust's collections is flexibility thanks to traits. Common traits like IntoIterator, FromIterator, Extend provide many options for populating and converting between types. Overall the collections aim for high performance while wrapping unsafe operations in safe interfaces that minimize risks, fitting nicely into Rust's overall design goals.
Рекомендации по теме