chapter four

4 Data structures

 

This chapter covers

  • Understanding Rust’s string types
  • Using Rust’s core data structures
  • Applying Rust’s core types effectively
  • Converting between data types
  • Handling foreign function interface (FFI) compatibility with Rust’s types

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 mainly discussed tooling; with that out of the way we can start talking about the language and its features, which we’ll focus on for the rest of this book. In this chapter we’ll cover the most important thing in Rust after 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 in 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.

We’ll discuss how Rust differs from other languages in its approach to data, review the different data types and structures, and discuss how to effectively use them. We’ll also discuss how Rust’s data types map to C types using the foreign function interface (FFI).

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

4.1.1 String vs str

4.1.2 Using strings effectively

4.1.3 Best practices for working with Rust strings

4.2 Understanding slices and arrays

4.3 Using vectors

4.3.1 Types related to vectors

4.3.2 Diving deeper into Vec

4.3.3 Wrapping vectors

4.3.4 Best practices for vectors

4.4 Using hash maps

4.4.1 Custom hashing functions

4.4.2 Creating hashable types

4.4.3 Best practices for hash maps

4.5 Understanding Rust’s types: primitives, structs, enums, aliases

4.5.1 Using primitive types

4.5.2 Using tuples

4.5.3 Using enums

4.5.4 Using aliases