Alternative representations Rust allows you to specify alternative data layout strategies from the default. repr(C) This is the most important repr. It has fairly simple intent: do what C does. The order, size, and alignment of fields is ex...
Exotically Sized Types Most of the time, we expect types to have a statically known and positive size. This isn't always the case in Rust. Dynamically Sized Types (DSTs) Rust supports Dynamically Sized Types (DSTs): types without a statical...
repr(Rust) First and foremost, all types have an alignment specified in bytes. The alignment of a type specifies what addresses are valid to store the value at. A value with alignment n must only be stored at an address that is a multiple o...
Data Representation in Rust Low-level programming cares a lot about data layout. It's a big deal. It also pervasively influences the rest of the language, so we're going to start by digging into how data is represented in Rust. This chapter...
Working with Unsafe Rust generally only gives us the tools to talk about Unsafe Rust in a scoped and binary manner. Unfortunately, reality is significantly more complicated than that. For instance, consider the following toy function: fn in...
What Unsafe Rust Can Do The only things that are different in Unsafe Rust are that you can: • Dereference raw pointers • Call unsafe functions (including C functions, compiler intrinsics, and the raw allocator) • Implement unsafe traits • A...
How Safe and Unsafe Interact What's the relationship between Safe Rust and Unsafe Rust? How do they interact? The separation between Safe Rust and Unsafe Rust is controlled with the unsafe keyword, which acts as an interface from one to the...
Meet Safe and Unsafe safe and unsafe It would be great to not have to worry about low-level implementation details. Who could possibly care how much space the empty tuple occupies? Sadly, it sometimes matters and we need to worry about it....
The Rustonomicon Warning: This book is incomplete. Documenting everything and rewriting outdated parts take a while. See the issue tracker to check what's missing/outdated, and if there are any mistakes or ideas that haven't been reported,...
Playground The Rust Playground is a way to experiment with Rust code through a web interface. Using it with mdbook In mdbook, you can make code examples playable and editable. fn main() { println!("Hello World!"); } This allows the reader t...
Documentation Use cargo doc to build documentation in target/doc, cargo doc --open will automatically open it in your web browser. Use cargo test to run all tests (including documentation tests), and cargo test --doc to only run documentati...
Meta Some topics aren't exactly relevant to how your program runs but provide you tooling or infrastructure support which just makes things better for everyone. These topics include: • Documentation: Generate library documentation for users...
Raw identifiers Rust, like many programming languages, has the concept of "keywords". These identifiers mean something to the language, and so you cannot use them in places like variable names, function names, and other places. Raw identifi...
Compatibility The Rust language is evolving rapidly, and because of this certain compatibility issues can arise, despite efforts to ensure forwards-compatibility wherever possible. • Raw identifiers
Inline assembly Rust provides support for inline assembly via the asm! macro. It can be used to embed handwritten assembly in the assembly output generated by the compiler. Generally this should not be necessary, but might be where the requ...
Unsafe Operations As an introduction to this section, to borrow from the official docs, "one should try to minimize the amount of unsafe code in a code base." With that in mind, let's get started! Unsafe annotations in Rust are used to bypa...
Development dependencies Sometimes there is a need to have dependencies for tests (or examples, or benchmarks) only. Such dependencies are added to Cargo.toml in the [dev-dependencies] section. These dependencies are not propagated to other...
Integration testing Unit tests are testing one module in isolation at a time: they're small and can test private code. Integration tests are external to your crate and use only its public interface in the same way any other code would. Thei...
Documentation testing The primary way of documenting a Rust project is through annotating the source code. Documentation comments are written in CommonMark Markdown specification and support code blocks in them. Rust takes care about correc...
Unit testing Tests are Rust functions that verify that the non-test code is functioning in the expected manner. The bodies of test functions typically perform some setup, run the code we want to test, then assert whether the results are wha...