doc.rust-lang.org
Rc<T>, the Reference-Counted Smart Pointer In the majority of cases, ownership is clear: You know exactly which variable owns a given value. However, there are cases when a single value might have multiple owners. For example, in graph data...
The Rust Programming Language
Book
2024-02-01
~6 min read
doc.rust-lang.org
Running Code on Cleanup with the Drop Trait The second trait important to the smart pointer pattern is Drop, which lets you customize what happens when a value is about to go out of scope. You can provide an implementation for the Drop trai...
The Rust Programming Language
Book
2024-02-01
~4 min read
doc.rust-lang.org
Treating Smart Pointers Like Regular References Implementing the Deref trait allows you to customize the behavior of the dereference operator * (not to be confused with the multiplication or glob operator). By implementing Deref in such a w...
The Rust Programming Language
Book
2024-02-01
~9 min read
doc.rust-lang.org
Using Box<T> to Point to Data on the Heap The most straightforward smart pointer is a box, whose type is written Box<T>. Boxes allow you to store data on the heap rather than the stack. What remains on the stack is the pointer to the heap d...
The Rust Programming Language
Book
2024-02-01
~9 min read
doc.rust-lang.org
Smart Pointers A pointer is a general concept for a variable that contains an address in memory. This address refers to, or “points at,” some other data. The most common kind of pointer in Rust is a reference, which you learned about in Cha...
The Rust Programming Language
Book
2024-02-01
~2 min read
doc.rust-lang.org
Extending Cargo with Custom Commands Cargo is designed so that you can extend it with new subcommands without having to modify it. If a binary in your $PATH is named cargo-something, you can run it as if it were a Cargo subcommand by runnin...
The Rust Programming Language
Book
2024-02-01
~1 min read
doc.rust-lang.org
Installing Binaries with cargo install The cargo install command allows you to install and use binary crates locally. This isn’t intended to replace system packages; it’s meant to be a convenient way for Rust developers to install tools tha...
The Rust Programming Language
Book
2024-02-01
~1 min read
doc.rust-lang.org
Cargo Workspaces In Chapter 12, we built a package that included a binary crate and a library crate. As your project develops, you might find that the library crate continues to get bigger and you want to split your package further into mul...
The Rust Programming Language
Book
2024-02-01
~8 min read
doc.rust-lang.org
Publishing a Crate to Crates.io We’ve used packages from crates.io as dependencies of our project, but you can also share your code with other people by publishing your own packages. The crate registry at crates.io distributes the source co...
The Rust Programming Language
Book
2024-02-01
~14 min read
doc.rust-lang.org
Customizing Builds with Release Profiles In Rust, release profiles are predefined, customizable profiles with different configurations that allow a programmer to have more control over various options for compiling code. Each profile is con...
The Rust Programming Language
Book
2024-02-01
~2 min read
doc.rust-lang.org
More About Cargo and Crates.io So far, we’ve used only the most basic features of Cargo to build, run, and test our code, but it can do a lot more. In this chapter, we’ll discuss some of its other, more advanced features to show you how to...
The Rust Programming Language
Book
2024-02-01
~1 min read
doc.rust-lang.org
Performance in Loops vs. Iterators To determine whether to use loops or iterators, you need to know which implementation is faster: the version of the search function with an explicit for loop or the version with iterators. We ran a benchma...
The Rust Programming Language
Book
2024-02-01
~2 min read
doc.rust-lang.org
Improving Our I/O Project With this new knowledge about iterators, we can improve the I/O project in Chapter 12 by using iterators to make places in the code clearer and more concise. Let’s look at how iterators can improve our implementati...
The Rust Programming Language
Book
2024-02-01
~5 min read
doc.rust-lang.org
Processing a Series of Items with Iterators The iterator pattern allows you to perform some task on a sequence of items in turn. An iterator is responsible for the logic of iterating over each item and determining when the sequence has fini...
The Rust Programming Language
Book
2024-02-01
~6 min read
doc.rust-lang.org
Closures Rust’s closures are anonymous functions you can save in a variable or pass as arguments to other functions. You can create the closure in one place and then call the closure elsewhere to evaluate it in a different context. Unlike f...
The Rust Programming Language
Book
2024-02-01
~14 min read
doc.rust-lang.org
Functional Language Features: Iterators and Closures Rust’s design has taken inspiration from many existing languages and techniques, and one significant influence is functional programming. Programming in a functional style often includes...
The Rust Programming Language
Book
2024-02-01
~1 min read
doc.rust-lang.org
Redirecting Errors to Standard Error At the moment, we’re writing all of our output to the terminal using the println! macro. In most terminals, there are two kinds of output: standard output (stdout) for general information and standard er...
The Rust Programming Language
Book
2024-02-01
~3 min read
doc.rust-lang.org
Working with Environment Variables We’ll improve the minigrep binary by adding an extra feature: an option for case-insensitive searching that the user can turn on via an environment variable. We could make this feature a command line optio...
The Rust Programming Language
Book
2024-02-01
~6 min read
doc.rust-lang.org
Adding Functionality with Test-Driven Development Now that we have the search logic in src/lib.rs separate from the main function, it’s much easier to write tests for the core functionality of our code. We can call functions directly with v...
The Rust Programming Language
Book
2024-02-01
~6 min read
doc.rust-lang.org
Refactoring to Improve Modularity and Error Handling To improve our program, we’ll fix four problems that have to do with the program’s structure and how it’s handling potential errors. First, our main function now performs two tasks: It pa...
The Rust Programming Language
Book
2024-02-01
~17 min read