liveBook · Manning

Chapter 16: Const, “unsafe” Rust, and external crates

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

The chapter introduces const generics and const functions, then previews unsafe Rust and external crates.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduces const generics and const functions, then previews unsafe Rust and external crates.

Key terms: const generics, const values, arrays, const functions, compile time

16.1 Const generics

This section explains how const generics let one definition work with arrays of different lengths, avoiding the need for separate structs for each size.

Key terms: type generics, lifetimes, const generics, arrays, array size, trait, Buffers, T, const N, usize

16.2 Const functions

This section explains that a const function can be called in a compile-time const context or used like a regular function, while the operations permitted in const functions depend on Rust's rules and version.

Key terms: const fn, const context, compile time, allocations

16.3 Mutable statics

This section explains how const-initialized synchronization types let Rust programs create a global collection that can be modified at run time.

Key terms: const context, const fn, static, Mutex, RwLock, Vec

16.4 Unsafe Rust

Introduces mutable statics, their program-wide changeability, and the requirement to use unsafe to access them.

Key terms: static mut, static, const, unsafe keyword

16.4.1 Overview of unsafe Rust

This section explains why Rust has unsafe code, what responsibility the keyword signals, and how an unsafe function is called.

Key terms: unsafe, memory address, unsafe keyword, unsafe block, unsafe fn

16.4.2 Using static mut in unsafe Rust

This section shows how `static mut` permits direct changes to a static value, why accessing it requires `unsafe`, and how concurrent changes can produce unexpected results.

Key terms: static mut, unsafe, Mutex, undefined behavior, threads, NUMBER, data races, const, static

16.4.3 Rust’s most famous unsafe method

This section shows how `transmute()` reinterprets a value’s bits as another type and why equal size alone does not make that safe.

Key terms: transmute(), i32, u32, User, undefined behavior

16.4.4 Methods ending in _unchecked

This section explains the risks of unchecked unsafe methods, questions their performance benefit, and puts unsafe Rust in perspective as an optional tool for specialized work.

Key terms: _unchecked, unwrap_unchecked(), Option, Result, Some, Ok, unsafe, low-level systems programmer, C libraries

16.5 Introducing external crates

Introduces external crates, explains why Rust programmers commonly use them, and notes how the Playground makes popular crates available.

Key terms: external crate, crates.io, external crates, Rust standard library, Playground

16.5.1 Crates and Cargo.toml

This section explains why rand is an external crate and how to add it to a Cargo project, run the project, and find the crate’s documentation.

Key terms: standard library, rand, blessed crates, Cargo.toml, [dependencies], cargo add rand, cargo run, std, crates.io, docs

16.5.2 Using the rand crate

Shows how to use the rand crate in the Playground to generate random numbers, including numbers from 1 to 10.

Key terms: Playground, crates, Cargo.toml, rand, random(), thread_rng(), rng, gen(), .gen_range()

16.5.3 Rolling some dice with rand

Uses a dice-based character creator to show how different random-roll rules affect game stats.

Key terms: d6, stat, Character struct, enum

16.6 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 15: Default, the builder pattern, and Deref Chapter 17: Rust’s most popular crates →

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.