liveBook · Manning

Chapter 20: A tour of the standard library

What you will learn in chapter 20 of Learn Rust in a Month of Lunches

Introduces a brief tour of the Rust standard library and previews the types and features the next sections will cover.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces a brief tour of the Rust standard library and previews the types and features the next sections will cover.

20.1 Arrays

Introduces upcoming changes that have made Rust arrays easier to work with.

20.1.1 Arrays now implement Iterator

This subsection explains that modern Rust can use arrays directly in for loops, unlike versions before Rust 1.53.

Key terms: arrays, Iterator, .iter(), slice, for loops

20.1.2 Destructuring and mapping arrays

This section shows how to extract values from arrays by destructuring and transform arrays with .map().

Key terms: destructuring, array, [], .map(), .collect()

20.1.3 Using from_fn to make arrays

Explains how `from_fn()` constructs an array with a closure and how the compiler determines the array’s length.

Key terms: from_fn(), closure, index, |_|, type inference, array length, assert_eq!, [&str; 5], [_; 5]

20.2 char

This section shows several useful char methods and explains why converting numbers to characters can fail.

Key terms: char, .escape_unicode(), Unicode, u8, u32, From, TryFrom, &str, len_utf(), convenience methods

20.3 Integers

Introduces the upcoming discussion of practical integer methods while noting that many mathematical methods are outside its scope.

20.3.1 Checked operations

This section explains how checked integer operations avoid overflow and panics, and why ordinary overflow behaves differently at compile time, in Debug mode, and in Release mode.

Key terms: .checked_add(), .checked_sub(), .checked_mul(), .checked_div(), Option, overflow, compile time, Debug mode, Release mode

20.3.2 The Add trait and other similar traits

This section explains how traits let custom Rust types use arithmetic and comparison operators, and clarifies the term rhs in integer-method documentation.

Key terms: rhs, right-hand side, Add, trait, +, #[derive(Add)], Sub, Mul, Div, AddAssign

20.4 Floats

Introduces float methods for producing whole-number-valued results and finding high or low values, including across an iterator.

Key terms: f32, f64, .floor(), .ceil(), .round(), .trunc(), .max(), .min(), .fold(), Iterator

20.5 Associated items and associated constants

Introduces associated items as items tied to a type or trait with `::`, noting that functions are one of three kinds.

Key terms: associated items, type, trait, :: double colon, functions

20.5.1 Associated functions

This section explains that methods are associated functions and that dot syntax is a convenient way to call them when they have a self parameter.

Key terms: associated function, self parameter, dot operator, :: syntax

20.5.2 Associated types

This section explains that a trait implementation defines its associated type and that the type can be referred to with a `::` signature.

Key terms: associated type, trait, Add trait, Output, associated type signature, :: double colon

20.5.3 Associated consts

This section shows how to define an associated const on a type and how a type can override an associated const supplied by a trait.

Key terms: associated const, Impl block, const, trait, override

20.6 bool

This section shows how Rust converts bool values to integers and uses bool methods to produce Option values.

Key terms: bool, .into(), Option, .then(), .then_some(), closure

20.7 Vec

Introduces several Vec methods for sorting, removing adjacent duplicates, splitting into slices, and draining a range.

Key terms: Vec, .sort(), .sort_unstable(), stable sorting, .dedup(), .split_at(), .split_at_mut(), slices, .drain(), iterator

20.8 String

This section shows how String shares several methods with Vec and how to manage its capacity and characters.

Key terms: String::with_capacity(), capacity, reallocate, push(), push_str(), length, shrink_to_fit(), retain(), closure, filter()

20.9 OsString and CString

Introduces std::ffi string types, explains why OsString is useful for operating-system strings, and shows how conversion to String handles failure.

Key terms: std::ffi, OsString, CString, OsStr, CStr, String, OsString::into_string(), Result, Err, unwrap

20.10 Summary

Concepts explained in this chapter

← Chapter 19: More crates and async Rust Chapter 21: Continuing the tour →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.