4 Data structures

 

This chapter covers

  • Using Rust’s core data structures: strings, vectors, and maps
  • Understanding Rust’s types: primitives, structs, enums, and aliases
  • Applying Rust’s core types effectively
  • Converting between data types
  • Demonstrating how Rust’s primitive types map to external libraries

Up to this point in the book, we haven’t spent much time talking about the Rust language itself. In the previous two chapters, we discussed tooling. With that out of the way, we can start diving into the Rust language and its features, which we’ll focus on for the rest of this book. In this chapter, we’ll cover the most important part of Rust after its basic syntax: data structures.

When working with Rust, you’ll spend a great deal of time interacting with its data structures, as you would any other language. Rust offers most of the features you’d expect from data structures, as you’d expect with any modern programming language, but it does so while offering exceptional safety and performance. Once you get a handle on Rust’s core data types, you’ll find the rest of the language comes into great clarity, as the patterns often repeat themselves.

In this chapter, we’ll discuss how Rust differs from other languages in its approach to data, review the core data types and structures, and discuss how to effectively use them. We’ll also discuss how Rust’s primitive types map to C types, which allows you to integrate with non-Rust software.

4.1 Demystifying String, str, &str, and &'static str

4.1.1 String vs str

4.1.2 Using strings effectively

4.2 Understanding slices and arrays

4.3 Vectors

4.3.1 Diving deeper into Vec

4.3.2 Wrapping vectors

4.3.3 Types related to vectors

4.4 Maps

4.4.1 Custom hashing functions

4.4.2 Creating hashable types

4.5 Rust types: Primitives, structs, enums, and aliases

4.5.1 Using primitive types

sitemap