liveBook · Manning

Chapter 18: Rust on your computer

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

This introduction explains why working on a computer offers capabilities beyond the Playground and identifies Cargo as Rust’s package manager.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This introduction explains why working on a computer offers capabilities beyond the Playground and identifies Cargo as Rust’s package manager.

Key terms: Playground, user input, command-line arguments, external crates, Cargo, package manager

18.1 Cargo

Introduces Cargo as the widely used tool that gives Rust projects a common structure for building and managing them.

Key terms: Cargo, common structure, external code

18.1.1 Why everyone uses Cargo

This section shows why Rust projects use Cargo instead of invoking rustc directly, then demonstrates how Cargo creates projects and builds programs with external crates.

Key terms: rustc, Cargo, crate, cargo new, Cargo.toml, src, main.rs, --lib, lib.rs, rand

18.1.2 Using Cargo and what Rust does while it compiles

This section explains why Rust compilation takes time, how Cargo supports development, and how debug and release builds trade compile time for runtime speed.

Key terms: ahead of time, incremental compilation, external crate, generic function, concrete, specialized definition, monomorphization, cargo build, cargo run, cargo check

18.2 Working with user input

Introduces two ways a Rust program can receive user input and distinguishes when each is supplied.

Key terms: stdin, command line arguments, user input

18.2.1 User input through stdin

This section shows how to read keyboard input with stdin and why trimming that input matters when comparing it with a typed character.

Key terms: std::io::stdin, Stdin, read_line, &mut String, {:?}, .trim(), whitespace, \r\n, \n

18.2.2 Accessing command-line arguments

This section shows how a Rust program reads command-line arguments, skips its own name, and uses the remaining strings as input.

Key terms: std::env::Args, std::env::args(), command-line arguments, IntoIterator, .skip(), index 1, capital, lowercase, match, CLI

18.2.3 Accessing environment variables

This section shows how to inspect and set environment variables, then uses logging settings to illustrate a practical fallback.

Key terms: environment variables, std::env::vars(), std::env::set_var(), key, value, RUST_LOG, TRACE, DEBUG, INFO, WARN

18.3 Using files

Introduces file work in Rust as an error-prone activity where Results and the ? operator are useful.

Key terms: Results, ? operator, files

18.3.1 Creating files

Introduces writing data to a new file, then shows shorter ways to do it and warns that std::fs::write replaces existing contents.

Key terms: std::fs, std::io::Write, .write_all(), question mark operator, std::fs::write(), &str, AsRef<[u8]>

18.3.2 Opening existing files

This subsection explains how to open an existing file and read its contents into a String.

Key terms: open(), create(), read_to_string(), String

18.3.3 Using OpenOptions to work with files

This section shows how to choose file-opening behavior with OpenOptions and explains the tradeoff between embedding a file with include_str! and reading it at run time.

Key terms: OpenOptions, File::options(), .open(), Result, builder pattern, .create_new(), .append(), .truncate(), include_str!, &'static str

18.4 cargo doc

This section shows how `cargo doc` generates browsable Rust documentation, including trait information and documentation comments.

Key terms: cargo doc, --open, --no-deps, automatically implemented, blanket implementations, methods, documentation comments, ///

18.5 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 17: Rust’s most popular crates Chapter 19: More crates and async Rust →

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.