This chapter sets up the core ideas and tools needed to build web services with Rust. It encourages readers to rely on the language’s compiler, documentation, and ecosystem rather than trying to memorize everything up front, and it frames the rest of the book around an example question-and-answer service that will eventually support creating, updating, deleting, and answering questions. The chapter also emphasizes that learning Rust for web development is really about learning how Rust thinks about types, memory, and asynchronous work, not just learning new syntax.
A large part of the chapter focuses on modeling application data with custom types and understanding how Rust’s strict type system shapes everyday programming. Using a question model as the example, it shows how to build structs, add behavior with implementation blocks, and use a newtype wrapper for identifiers to give primitive values more meaning. It then explains optional data with the option type, distinguishes between owned strings and string slices, and walks through Rust’s ownership, borrowing, moving, and mutability rules. Compiler errors are treated as learning opportunities, and the chapter shows how to use documentation and trait information to convert values, handle results, and implement display and debug formatting for custom types.
The final part introduces asynchronous programming and the infrastructure needed to run a web server. It explains that Rust provides async and await syntax plus the future type, but no built-in runtime, so developers choose an ecosystem component such as Tokio to manage polling, execution, and kernel interaction. The chapter then surveys common Rust web frameworks and selects Warp as the book’s framework of choice, noting that it sits on top of Tokio and Hyper. By the end, readers have a working minimal server and a stronger understanding of the language features and ecosystem decisions that will support the rest of the book.
Roadmap for this chapter to unlock your capabilities to become a Rust web developer
Custom types can be created with structs, and adding custom methods is done via the impl block.
The Rust function signature in detail when returning a value
An associated function (new, at the top) doesn’t take an &self parameter and is called via double colons (::). A method (update_title, at the bottom) takes an &self parameter and is called via dot notation (.). Calling a function from within an impl block is possible via the name of the block (Question::new(...), in this case).
Although it’s complex, the Rust documentation is fast to navigate and offers tons of information.
Primitive types are stored on the stack in Rust, whereas more complex types are stored on the heap. String and &str point to more complex data types (collections of UTF-8 values).Whereas &str has a fat pointer (the memory address and the length field) showing the location on the heap, the String pointer has not only an address and length field, but also a capacity field.
When reassigning complex types to a new variable, Rust copies the pointer information and gives ownership to the new variable. The old one is not needed anymore and goes out of scope.
The sidebar offers every method available to a certain type, and finding the implementation details requires a bit of digging sometimes.
An asynchronous programming environment needs four ingredients (syntax, type, runtime, and kernel abstractions) for it to function.
The building blocks of the async Rust ecosystem
The complete asynchronous Rust environment
Executing async HTTP requests behind the scenes
When using Warp, you inherit the runtime Tokio and Hyper as the HTTP abstraction and server under the hood.
Summary
- Always start by mapping out resources via structs and think about the relationships between your types.
- Simplify your life by adding helper methods like new onto your types and transforming one type to another.
- Understand the ownership and borrowing principles in Rust and how they affect the way you write code, and which errors the compiler might therefore throw at you.
- Using traits helps you make your custom data types play nicely with the frameworks you are choosing, by adding functionality.
- Using the derive macro to implement traits for common use cases saves a lot of self-written code.
- Befriend the Rust documentation, as you will use it often to look up functionalities in types and frameworks. This, in turn, helps you better understand the language.
- Rust ships with the async syntax and types, but we need more to write async applications.
- Runtimes take care of handling multiple computations at the same time and provide an abstraction over the async kernel API at the same time.
- Choose a web framework that is actively maintained, has a large community and support around it, and is maybe used by larger companies (different from the ones you operate under).
- The web framework we choose will abstract over the HTTP implementation, server, and runtime so we can focus on writing business logic for the application.
FAQ
What is the main purpose of Chapter 2, “Laying the foundation”?
The chapter builds the core Rust foundations you need for web development: custom types, ownership, traits, options/results, documentation usage, async basics, and choosing a web framework. It ends by setting up a basic working web server.Why does the book recommend learning the first chapters of “The Rust Programming Language” first?
Those chapters provide the language foundation needed to understand the concepts used throughout the book. You may still follow along without them, but the book recommends at least a brief read for a stronger understanding of Rust.What custom data types are introduced for the Q&A application?
The application starts with three main types: Users, Questions, and Answers. The chapter focuses first on modeling the Question type.Why does the book use a newtype like QuestionId instead of a plain string?
Wrapping a string in a dedicated QuestionId type gives the ID more meaning, helps prevent mixing it up with other strings, and lets the compiler enforce correct usage.When should I use String versus &str?
Use String when you need ownership and want to modify the text. Use &str when you only need to read or borrow text without taking ownership.Why is Option used for the tags field in Question?
Option makes the field optional, so a question can exist with or without tags. It also forces you to handle both cases explicitly: Some and None.What is the difference between an associated function and a method in Rust?
An associated function does not take&self and is called with ::, like Question::new(). A method takes &self and is called with dot notation, like value.method().Why did the code need a Display or Debug implementation?
println!("{}", value) requires the Display trait, while complex structures are often printed with {:?}, which uses Debug. Since custom structs do not implement these automatically, you must derive or implement them.What role do traits like FromStr play in this chapter?
Traits provide shared behavior in Rust. In this chapter, FromStr is used to convert a string slice into a QuestionId, and it returns a Result to handle possible failure.What are the main ingredients of Rust’s async ecosystem?
The chapter identifies four ingredients: async/await syntax, theFuture type, a runtime such as Tokio, and kernel-level async I/O support through abstractions like Mio. Together, they make nonblocking web servers possible.
Rust Web Development ebook for free