Building a Single-Threaded Web Server We’ll start by getting a single-threaded web server working. Before we begin, let’s look at a quick overview of the protocols involved in building web servers. The details of these protocols are beyond...
From a Single-Threaded to a Multithreaded Server Right now, the server will process each request in turn, meaning it won’t process a second connection until the first connection is finished processing. If the server received more and more r...
Graceful Shutdown and Cleanup The code in Listing 21-20 is responding to requests asynchronously through the use of a thread pool, as we intended. We get some warnings about the workers, id, and thread fields that we’re not using in a direc...
Appendix The following sections contain reference material you may find useful in your Rust journey.
Appendix A: Keywords The following lists contain keywords that are reserved for current or future use by the Rust language. As such, they cannot be used as identifiers (except as raw identifiers, as we discuss in the “Raw Identifiers” secti...
Appendix B: Operators and Symbols This appendix contains a glossary of Rust’s syntax, including operators and other symbols that appear by themselves or in the context of paths, generics, trait bounds, macros, attributes, comments, tuples,...
Appendix C: Derivable Traits In various places in the book, we’ve discussed the derive attribute, which you can apply to a struct or enum definition. The derive attribute generates code that will implement a trait with its own default imple...
Appendix D: Useful Development Tools In this appendix, we talk about some useful development tools that the Rust project provides. We’ll look at automatic formatting, quick ways to apply warning fixes, a linter, and integrating with IDEs. A...
Appendix E: Editions In Chapter 1, you saw that cargo new adds a bit of metadata to your Cargo.toml file about an edition. This appendix talks about what that means! The Rust language and compiler have a six-week release cycle, meaning user...
Appendix F: Translations of the Book For resources in languages other than English. Most are still in progress; see the Translations label to help or let us know about a new translation! • Português (BR) • Português (PT) • 简体中文: KaiserY/trp...
Appendix G - How Rust is Made and “Nightly Rust” This appendix is about how Rust is made and how that affects you as a Rust developer. Stability Without Stagnation As a language, Rust cares a lot about the stability of your code. We want Ru...
Rust by Example Rust is a modern systems programming language focusing on safety, speed, and concurrency. It accomplishes these goals by being memory safe without using garbage collection. Rust by Example (RBE) is a collection of runnable e...
Hello World This is the source code of the traditional Hello World program. // This is a comment, and is ignored by the compiler. // You can test this code by clicking the "Run" button over there -> // or if you prefer to use your keyboard,...
Comments Any program requires comments, and Rust supports a few different varieties: Regular Comments These are ignored by the compiler: • Line comments: Start with // and continue to the end of the line • Block comments: Enclosed in /* ......
Formatted print Printing is handled by a series of macros defined in std::fmt some of which are: • format!: write formatted text to String • print!: same as format! but the text is printed to the console (io::stdout). • println!: same as pr...
Debug All types which want to use std::fmt formatting traits require an implementation to be printable. Automatic implementations are only provided for types such as in the std library. All others must be manually implemented somehow. The f...
Display fmt::Debug hardly looks compact and clean, so it is often advantageous to customize the output appearance. This is done by manually implementing fmt::Display, which uses the {} print marker. Implementing it looks like this: // Impor...
Testcase: List Implementing fmt::Display for a structure where the elements must each be handled sequentially is tricky. The problem is that each write! generates a fmt::Result. Proper handling of this requires dealing with all the results....
Formatting We've seen that formatting is specified via a format string: • format!("{}", foo) -> "3735928559" • format!("0x{:X}", foo) -> "0xDEADBEEF" • format!("0o{:o}", foo) -> "0o33653337357" The same variable (foo) can be formatted diffe...
Primitives Rust provides access to a wide variety of primitives. A sample includes: Scalar Types • Signed integers: i8, i16, i32, i64, i128 and isize (pointer size) • Unsigned integers: u8, u16, u32, u64, u128 and usize (pointer size) • Flo...