chapter twenty

20 A tour of the standard library

 

This chapter covers

  • Review and more in-depth look at types you already know such as arrays, char, bool, numeric types, Vec, and String
  • Associated constants
  • A summary of the three associated items in Rust
  • Recently added functions such as from_fn and then_some
  • Various types you haven’t seen before like OsString and CString

Good work! You’re almost through the book and there are only five chapters left. For this chapter and the next we are now going to sit back and relax and go on a short tour of the standard library, including further detail on some of the types we already know. You will certainly end up encountering these modules and methods as you continue to use Rust, so we might as well learn them now so that they are already familiar to you. Nothing in this chapter will be particularly difficult to learn, and we’ll keep things pretty brief and run through one type per section.

20.1 Arrays

Arrays have become easier to work with over time, as we saw in the chapter on const generics. Some other nice changes have taken place that we'll take a look at now.

20.1.1 Arrays now implement Iterator

In the past (before Rust 1.53), arrays didn't implement Iterator and you needed to use methods like .iter() on them in for loops. (Another method was to use & to get a slice in for loops). So the following code didn't work in the past:

20.1.2 Destructuring and mapping arrays

20.1.3 Using from_fn to make arrays

20.2 char

20.3 Integers

20.3.1 Checked operations

20.3.2 The Add trait and other similar traits

20.4 Floats

20.5 Associated items and associated constants

20.5.1 Associated functions

20.5.2 Associated types

20.5.3 Associated consts

20.6 bool

20.7 Vec

20.8 String

20.9 OsString and CString

20.10 Summary