Rust is a systems programming language designed for performance, safety, and concurrency that aims to provide a balance between low-level control and high-level, zero-cost abstractions.
A foundational goal of Rust is to ensure that your programs never have undefined behavior. That is the meaning of "safety". Undefined behavior is especially dangerous for low-level programs with direct access to memory. The Rust compiler guarantees memory safety without a garbage collector or additional runtime cost.
~70% of the vulnerabilities Microsoft assigns a CVE each year continue to be memory safety issues.
A secondary goal of Rust is to detect and prevent undefined behavior at compile-time. By moving the detection of bugs earlier in the development process, Rust aims to improve the quality of software by preventing bugs from reaching production.
Zero Cost Abstractions - the ability to move certain behaviors to compile time execution or analysis. Rust's high-level abstractions don鈥檛 incur runtime overhead.
Rust achieves these goals through its ownership system - a set of rules that the compiler checks and enforces. The ownership system is key to understanding and programming in Rust. It is a discipline for managing memory that ensures memory is used correctly and safely. Later we will examine how the borrow checker enforces these rules and how lifetimes ensure references are valid throughout their usage.
Rust has a rich type system that allows for expressive and concise code. It is syntactically is similar to C++, but with modern features like pattern matching, traits, and generics.
Similarly, handling concurrent programming safely and efficiently is another of Rust's major goals. The ownership model and string type system help manage memory safety and concurrency problems!
In summary, Rust combines low-level control with high-level abstractions, making it an excellent choice for embedded systems programming all the way up to web development and beyond.
Rust comes with a package manager and build tool called Cargo. Cargo simplifies the process of building, testing, and managing Rust projects. It handles dependencies, compiles code, and manages project configuration.
Cargo is a powerful tool that streamlines the development process and ensures consistent project structure. It is an essential part of the Rust ecosystem and is used by developers to create, build, and share Rust projects.
Rust libraries are called crates. Cargo supports crates in source form or compressed packages fetched from a registry.
Cargo downloads and compiles crates from the crates.io registry by default. You can also specify dependencies from other sources, such as internal mirrors or private repositories.
Rust crate source code are organized logically as modules. Modules are used to organize related functionality, as well as control visible scope of code symbols.
Cargo supports running unit and integration tests. Unit tests, which include documentation tests, are typically colocated with product code. Integration tests are located in tests/ folder. Tests can be run using cargo test or other community tools (such as nextest).
Tests are generally wrapped within a module annotated with the cfg(tests) macro. A quick way to get started with Rust can be to author tests (even without any production code), as follows:
#[cfg(test)]
mod tests {
use std::mem::size_of;
#[test]
fn u16_size() {
assert_eq!(size_of::<u16>(), 2);
}
#[test]
fn i32_size() {
assert_eq!(size_of::<i32>(), 4);
}
#[test]
fn bool_size() {
assert_eq!(size_of::<bool>(), 1);
}
}Running tests using the standard Rust cargo test tooling is the most common way to test without downloading additional tooling. The parameters passed into cargo test typically include the same parameters as the cargo build command (used to build the production code).
Nextest is an evolved version of cargo test test driver that lists and runs individual tests in parallel (cargo test runs tests in sequence and fails when exit code is non-zero). It also provides provides detailed test run information that is useful for large-scale CI systems.
Rust has a rich ecosystem of tools that enhance the development experience. The rustc compiler produces efficient machine code, and the rustdoc tool generates documentation from source code comments. The Rust Language Server (RLS) provides IDE support for code completion, refactoring, and error checking.
rustup is the easiest way to install and manage Rust and its developer tools:
- rustc - the Rust compiler
- cargo - the package manager and build tool
- rustdoc - the documentation generator
- rustfmt - the code formatter. It formats Rust code according to the official style guidelines.
- clippy - a collection of lints to catch common mistakes and improve code quality.
The Rust Playground is an online tool for experimenting with Rust code without installing anything locally.
Other tools like Rust Analyzer provide advanced IDE support for Rust development. All of these tools are configured and available in the official Rust devcontianer.
Rust has excellent documentation that covers the language, standard library, and ecosystem. The official Rust Book is a comprehensive guide to learning Rust from scratch. The Rust Reference provides detailed information about the language syntax and semantics.
Rust's documentation is generated from source code comments using the rustdoc tool. Developers can write detailed explanations and examples using Markdown syntax in their code. Crate documentation is published on crates.io and can be viewed online or offline using cargo doc.