liveBook · Manning

Chapter 7: Traits: Making different types do the same thing

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

This introduction previews the chapter’s trait topics without explaining how traits work.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction previews the chapter’s trait topics without explaining how traits work.

7.1 Traits: The basics

Introduces traits as guarantees of a type’s behavior, contrasts derived and manual implementations, and shows why an implementation must follow a trait method’s signature.

Key terms: trait, implement, methods, #[derive(Debug)], Add, impl, derive, method signature, fn run(&self)

7.1.1 All you need are the method signatures

This section explains when trait methods need only signatures, then uses a custom Display implementation to show how traits give types shared abilities.

Key terms: trait, method signature, default method, Display, Debug, Cat, write!, ToString, blanket implementation, .to_string()

7.1.2 More complex examples

This section uses a game example to show how trait bounds make default methods useful and how trait-based parameters can replace a concrete monster type.

Key terms: &self, trait bounds, Debug, {:?}, MonsterBehavior, .take_damage(), .display_self(), &mut self, Monster, concrete type

7.1.3 Traits as bounds

This section explains that a trait can have no methods and still restrict which types a function accepts.

Key terms: trait, trait bound, impl

7.1.4 Traits are like qualifications

This section presents traits as qualifications that types can have and shows how functions use those qualifications as bounds.

Key terms: trait, implement, qualification, bound

7.2 The From trait

The section uses Vec conversions to show how From converts between types and how to define a conversion for your own types.

Key terms: From, City, Country, Vec::from(), &str, String, Vec<u8>, bytes

7.3 The orphan rule

This section explains which trait implementations Rust’s orphan rule permits, why the restriction exists, and the basic newtype workaround.

Key terms: orphan rule, trait, type, Display, tuple struct, newtype idiom

7.4 Getting around the orphan rule with newtypes

The section shows how wrapping another type in a tuple struct creates a new type on which you can implement traits, while keeping the wrapped type’s traits and methods separate.

Key terms: newtype idiom, tuple struct, traits, #[derive], impl block, File, String, .0, pub

7.5 Taking a String and a &str in a function

Shows how to accept both String and &str in a generic function by requiring AsRef<str> and calling .as_ref() when a &str is needed.

Key terms: String, &str, AsRef<str>, trait bound, Display, .as_ref()

7.6 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 6: More collections, more error handling Chapter 8: Iterators and closures →

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.