Chapter 1: Why Rust?
This introduction presents Rust’s performance, safety, tooling, and versatility as reasons to use it for web development and other systems.
After this chapter you will be able to
- Compiled performance and safety. Explain how Rust combines performance with compiler-enforced safety.
- Cargo project build workflow. Describe how Cargo creates, builds, and runs a project, including where its source and debug output reside.
- Cargo dependency and lockfiles. Explain how Cargo.toml and Cargo.lock work together to fetch dependencies and reproduce their versions.
- Compilation and performance tradeoffs. Explain how Rust combines C-like speed with compile-time memory safety, and contrast its runtime tradeoff with Go’s.
- Exhaustive match checking. Describe how the compiler checks the example match block and what happens when the true case is removed.
- Compiler feedback and workflow. Explain how compiler diagnostics support the Rust development and learning workflow.
- TCP and HTTP provision. Distinguish Rust’s built-in TCP support from HTTP support and explain how a web framework supplies the latter.
- Asynchronous request handling. Explain why asynchronous programming helps a web server handle multiple requests.
- Asynchronous stack boundaries. Describe the main building blocks for asynchronous work and which ones Rust leaves to the community.
- Runtime selection and composition. Explain how frameworks select asynchronous runtimes and why an application can contain more than one.
- Futures and active execution. Explain what an async function returns, what the Future trait describes, and how its work is started.
- Built-in documentation and examples. Explain how Cargo generates documentation from code comments and how testing embedded examples helps maintain them.
- Modularization and Cargo dependencies. Describe how modularization groups code or extracts reusable code into a crate, and how Cargo.toml can include libraries from different locations.
- Standardized testing and team friction. Explain Rust’s built-in support for creating and running tests and why standardized tooling can reduce team friction.
Section by section
Introduction
This introduction presents Rust’s performance, safety, tooling, and versatility as reasons to use it for web development and other systems.
- Compiled performance and safety: Explain how Rust combines performance with compiler-enforced safety.
Why it matters: Rust’s compiler helps deliver both speed and safety without relying on garbage collection. - Built-in productivity tooling: Describe how Rust’s built-in tooling supports building and maintaining applications.
Why it matters: Rust’s tooling supports productivity as well as the language’s performance and safety. - Range of Rust applications: Describe the range of systems for which Rust can be used.
Why it matters: Rust experience can apply across more than one kind of development project.
Key terms: compiler, garbage collection, memory safe, use-after-free, testing, documentation, package manager, systems programming, WebAssembly
1.1 Batteries included: Rust’s tooling
This section introduces Rust’s tools for installing, building, maintaining, and managing dependencies in applications.
- Rustup installation and updates: Describe how Rustup helps install and update Rust and add helper components.
Why it matters: Rustup gives you a way to maintain your Rust installation and its tools. - Cargo project build workflow: Describe how Cargo creates, builds, and runs a project, including where its source and debug output reside.
Why it matters: This workflow takes a new Rust project from source code to a running program. - Rust compilation toolchain: Trace the section’s description of compilation from Rustc through LLVM to machine code.
Why it matters: The compiler toolchain explains how Rust source becomes code an operating system can run. - Cargo dependency and lockfiles: Explain how Cargo.toml and Cargo.lock work together to fetch dependencies and reproduce their versions.
Why it matters: Dependency declarations and locked versions help collaborators build the same project consistently. - Lockfile version control guidance: Distinguish the section’s Cargo.lock version-control advice for applications and libraries and explain its rationale.
Why it matters: The section’s advice connects lockfile practices to the different needs of applications and libraries. - Formatting and linting tools: Describe Rustfmt’s formatting role and identify Clippy as Rust’s official linter.
Why it matters: These tools support consistent formatting and linting as a codebase grows.
Key terms: Rustup, rustup update, Cargo, cargo new, cargo run, cargo build, src, target/debug, Rustc, LLVM
1.2 The Rust compiler
This section explains how Rust’s compiler supports performance, safety, development, and maintenance.
- Compilation and performance tradeoffs: Explain how Rust combines C-like speed with compile-time memory safety, and contrast its runtime tradeoff with Go’s.
Why it matters: This tradeoff helps explain why Rust can offer both speed and memory safety. - Exhaustive match checking: Describe how the compiler checks the example match block and what happens when the true case is removed.
Why it matters: This example shows how the compiler catches an omitted case before a program runs. - Compiler feedback and workflow: Explain how compiler diagnostics support the Rust development and learning workflow.
Why it matters: Reading compiler feedback is part of building and learning Rust applications. - Maintenance and team benefits: Explain why compiler checks can help maintain larger systems and establish a baseline for team contributions.
Why it matters: Compiler checks can make changes and team contributions easier to manage.
Key terms: binary code, garbage collection, memory safety, compile time, runtime, match block, pattern matching, use case, compiler error, error messages
1.3 Rust for web services
This section explains Rust’s web-service stack, asynchronous programming foundations, and the trade-offs of relying on community implementations.
- TCP and HTTP provision: Distinguish Rust’s built-in TCP support from HTTP support and explain how a web framework supplies the latter.
Why it matters: This distinction explains why a Rust web service needs an HTTP implementation beyond the standard library. - Asynchronous request handling: Explain why asynchronous programming helps a web server handle multiple requests.
Why it matters: This gives the practical reason for using async in a web service. - Asynchronous stack boundaries: Describe the main building blocks for asynchronous work and which ones Rust leaves to the community.
Why it matters: Knowing these boundaries helps you understand what Rust provides and what an async framework must add. - Runtime selection and composition: Explain how frameworks select asynchronous runtimes and why an application can contain more than one.
Why it matters: Dependency choices can determine which schedulers end up in your application. - Futures and active execution: Explain what an async function returns, what the Future trait describes, and how its work is started.
Why it matters: This explains why calling an async function alone does not start its work. - Web service trade-offs: Explain the section’s argument for using Rust for web services despite its lack of standard HTTP and async runtime implementations.
Why it matters: The trade-off shows why depending on community components can coexist with strong reasons to choose Rust.
Key terms: TCP, HTTP, standard library, web framework, Hyper, asynchronous programming, requests, syntax, type, thread scheduler
1.4 Maintainability of Rust applications
This section explains how Rust’s documentation, modularization, testing, and community resources support maintainable applications.
- Built-in documentation and examples: Explain how Cargo generates documentation from code comments and how testing embedded examples helps maintain them.
Why it matters: Documentation and tested examples help future maintainers understand and use the code. - Modularization and Cargo dependencies: Describe how modularization groups code or extracts reusable code into a crate, and how Cargo.toml can include libraries from different locations.
Why it matters: Organizing and reusing code makes a codebase easier to maintain. - Standardized testing and team friction: Explain Rust’s built-in support for creating and running tests and why standardized tooling can reduce team friction.
Why it matters: Built-in testing and documentation tools let teams spend less time choosing tools and more time building software. - Community help resources: Describe where to seek guidance when a team lacks expertise, using Tokio and Warp as examples.
Why it matters: Community resources offer another place to find help when expertise is missing from the team.
Key terms: Cargo, documentation, code comments, tests, modularizing, crate, Cargo.toml, dependency section, crates.io, testing
1.5 Summary
Where this is used later
- Creating our web server (chapter 2) · Creating the web server relies on building and running a Rust project.
- Development vs. release flag when building your binary (chapter 10) · Comparing development and release builds extends the basic Cargo build workflow.
- Cross-compile your binary for different environments (chapter 10) · Cross-compiling builds on the idea that Rust targets different environments.
- Choosing a web framework (chapter 2) · Choosing a web framework leads to using a third-party dependency in the project.
- Adding SQLx into our project (chapter 7) · Adding SQLx relies on declaring and fetching a crate.
- Installing and using Clippy (chapter 5) · Using Clippy builds on its identification as the Rust linter.
- Formatting your code with Rustfmt (chapter 5) · Formatting with Rustfmt expands on the basic cargo fmt use.
- Handling results (chapter 2) · Handling results builds on attending to the cases code must handle.
Concepts explained in this chapter
- async runtimeAn asynchronous runtime schedules and drives asynchronous tasks, managing their progress and associated execution resources.
- async syntaxAsync syntax is Rust syntax for marking functions or code blocks as asynchronous computations.
- CargoCargo is Rust’s package manager and build tool.
- rustupRustup is the toolchain installer and version manager for Rust.
- documentationSoftware documentation is written information describing software, its behavior, or how to use it.
- software testingSoftware testing is the process of evaluating software to find defects and verify expected behavior.
- Future::OutputFuture::Output is the associated type specifying the value produced when a future completes.
- await syntaxAwait syntax in Rust suspends an asynchronous computation until its future can make progress or complete.
- pollFuture::poll is the trait method a runtime calls to advance a future's computation.
- cargo clippycargo clippy is a Cargo command that runs Clippy on a Rust package.
- crateA crate is a Rust package unit that can be compiled and distributed as a library or executable.
- request messageAn HTTP request is a message sent by a client to a server using the Hypertext Transfer Protocol.
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.
