liveBook · Manning

Chapter 3: Compound data types

What you will learn in chapter 3 of Rust in Action: Systems programming concepts and techniques

The introduction explains why Rust’s structs and enums are useful for composing simpler values into compound data types.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why Rust’s structs and enums are useful for composing simpler values into compound data types.

Key terms: struct, enum, compound data types

3.1 Using plain functions to experiment with an API

The section sketches a file API with plain functions, identifies what that sketch cannot yet represent, and explains Rust’s unit and Never return types.

Key terms: type alias, String, read(), bool, methods, unit type, (), zero-length tuple, semicolon, Never type

3.2 Modeling files with struct

The section uses a struct to model an in-memory file, shows how a newtype creates a distinct type, and identifies what the mock file still cannot do.

Key terms: struct, File, field, Vec<u8>, instance, literal syntax, field access, reference, borrowing, type

3.3 Adding methods to a struct with impl

The section introduces Rust methods, their dot-call syntax, and the separate impl blocks used to define them.

Key terms: methods, functions, dot operator, impl block, struct, enum, class keyword, inheritance

3.3.1 Simplifying object creation by implementing new()

This subsection explains why Rust programmers commonly use a new() method to create structs with reasonable defaults instead of repeating struct literals.

Key terms: new(), struct, literal syntax, impl block

3.4 Returning errors

This section explains why file operations need to signal failures more informatively than a boolean result can.

Key terms: read(), open(), close(), bool, error message

3.4.1 Modifying a known global variable

This section explains global error codes as a systems-programming convention, why they are risky in Rust, and how const differs from an immutable let binding.

Key terms: errno, close(), EINTR, EIO, global variable, type safety, OS-defined global values, flags, const, let

3.4.2 Making use of the Result return type

This section shows how Result represents success or failure in file operations, what unwrap() does with each case, and why the example rebinds a File value after passing ownership.

Key terms: Result, Ok, Err, Result<File, String>, unwrap(), ownership, File, f4, rebound

3.5 Defining and making use of an enum

Introduces Rust enums as types with known variants and explains why they are useful for representing event codes instead of strings.

Key terms: enum, enumeration, variants, event codes, string comparisons, pattern-matching, impl blocks, methods

3.5.1 Using an enum to manage internal state

The section applies enums to a file’s open-or-closed state and recommends them over stringly-typed codes when modeling such distinctions.

Key terms: enum, File type, stringly-typed, message codes

3.6 Defining common behavior with traits

The section introduces traits as rules for shared behavior, using medium-independent file operations and the familiar Debug trait as examples.

Key terms: file, reading, writing, streams of bytes, trait, Debug trait, #[derive(Debug)]

3.6.1 Creating a Read trait

This subsection explains how traits express shared behavior across types, distinguishes defining a trait from implementing it, and uses PartialEq as an example.

Key terms: trait, Read trait, code reuse, impl, PartialEq, ==, NAN, NULL

3.6.2 Implementing std::fmt::Display for your own types

This section explains how Rust’s formatting macros use traits and how to give a custom type its own printed representation by implementing Display.

Key terms: println!, Display, Debug, fmt, fmt::Result, impl Trait for Type

3.7 Exposing your types to the world

Introduces the value of hiding internal crate details and documenting public ones, then previews tooling covered later in the section.

3.7.1 Protecting private data

This subsection explains that Rust keeps items private by default and uses `pub` to expose them from a library.

Key terms: private, pub, public, crate

3.8 Creating inline documentation for your projects

The section introduces inline documentation and explains how Rust’s two doc-comment forms attach documentation to different items.

Key terms: doc comments, ///, //!, module

3.8.1 Using rustdoc to render docs for a single source file

This subsection shows how to generate HTML documentation from a single Rust source file with rustdoc.

Key terms: rustdoc, inline documentation, HTML, doc/

3.8.2 Using cargo to render docs for a crate and its dependencies

This subsection shows how to render a crate’s documentation as HTML with Cargo and notes two ways to improve the output or build process.

Key terms: crate, cargo doc --open, HTML, cargo doc --no-deps, dependencies, rustdoc, Markdown, triple backticks, syntax highlighting

3.9 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 2: Language foundations Chapter 4: Lifetimes, ownership, and borrowing →

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.