doc.rust-lang.org
Bringing Paths into Scope with the use Keyword Having to write out the paths to call functions can feel inconvenient and repetitive. In Listing 7-7, whether we chose the absolute or relative path to the add_to_waitlist function, every time...
The Rust Programming Language
Book
2024-02-01
~7 min read
doc.rust-lang.org
Paths for Referring to an Item in the Module Tree To show Rust where to find an item in a module tree, we use a path in the same way we use a path when navigating a filesystem. To call a function, we need to know its path. A path can take t...
The Rust Programming Language
Book
2024-02-01
~10 min read
doc.rust-lang.org
Control Scope and Privacy with Modules In this section, we’ll talk about modules and other parts of the module system, namely paths, which allow you to name items; the use keyword that brings a path into scope; and the pub keyword to make i...
The Rust Programming Language
Book
2024-02-01
~5 min read
doc.rust-lang.org
Packages and Crates The first parts of the module system we’ll cover are packages and crates. A crate is the smallest amount of code that the Rust compiler considers at a time. Even if you run rustc rather than cargo and pass a single sourc...
The Rust Programming Language
Book
2024-02-01
~2 min read
doc.rust-lang.org
Packages, Crates, and Modules As you write large programs, organizing your code will become increasingly important. By grouping related functionality and separating code with distinct features, you’ll clarify where to find code that impleme...
The Rust Programming Language
Book
2024-02-01
~2 min read
doc.rust-lang.org
Concise Control Flow with if let and let...else The if let syntax lets you combine if and let into a less verbose way to handle values that match one pattern while ignoring the rest. Consider the program in Listing 6-6 that matches on an Op...
The Rust Programming Language
Book
2024-02-01
~4 min read
doc.rust-lang.org
The match Control Flow Construct Rust has an extremely powerful control flow construct called match that allows you to compare a value against a series of patterns and then execute code based on which pattern matches. Patterns can be made u...
The Rust Programming Language
Book
2024-02-01
~9 min read
doc.rust-lang.org
Defining an Enum Where structs give you a way of grouping together related fields and data, like a Rectangle with its width and height, enums give you a way of saying a value is one of a possible set of values. For example, we may want to s...
The Rust Programming Language
Book
2024-02-01
~11 min read
doc.rust-lang.org
Enums and Pattern Matching In this chapter, we’ll look at enumerations, also referred to as enums. Enums allow you to define a type by enumerating its possible variants. First we’ll define and use an enum to show how an enum can encode mean...
The Rust Programming Language
Book
2024-02-01
~1 min read
doc.rust-lang.org
Methods Methods are similar to functions: We declare them with the fn keyword and a name, they can have parameters and a return value, and they contain some code that’s run when the method is called from somewhere else. Unlike functions, me...
The Rust Programming Language
Book
2024-02-01
~8 min read
doc.rust-lang.org
An Example Program Using Structs To understand when we might want to use structs, let’s write a program that calculates the area of a rectangle. We’ll start by using single variables and then refactor the program until we’re using structs i...
The Rust Programming Language
Book
2024-02-01
~7 min read
doc.rust-lang.org
Defining and Instantiating Structs Structs are similar to tuples, discussed in “The Tuple Type” section, in that both hold multiple related values. Like tuples, the pieces of a struct can be different types. Unlike with tuples, in a struct...
The Rust Programming Language
Book
2024-02-01
~8 min read
doc.rust-lang.org
Using Structs to Structure Related Data A struct, or structure, is a custom data type that lets you package together and name multiple related values that make up a meaningful group. If you’re familiar with an object-oriented language, a st...
The Rust Programming Language
Book
2024-02-01
~1 min read
doc.rust-lang.org
The Slice Type Slices let you reference a contiguous sequence of elements in a collection. A slice is a kind of reference, so it does not have ownership. Here’s a small programming problem: Write a function that takes a string of words sepa...
The Rust Programming Language
Book
2024-02-01
~9 min read
doc.rust-lang.org
References and Borrowing The issue with the tuple code in Listing 4-5 is that we have to return the String to the calling function so that we can still use the String after the call to calculate_length, because the String was moved into cal...
The Rust Programming Language
Book
2024-02-01
~6 min read
doc.rust-lang.org
What Is Ownership? Ownership is a set of rules that govern how a Rust program manages memory. All programs have to manage the way they use a computer’s memory while running. Some languages have garbage collection that regularly looks for no...
The Rust Programming Language
Book
2024-02-01
~18 min read
doc.rust-lang.org
Understanding Ownership Ownership is Rust’s most unique feature and has deep implications for the rest of the language. It enables Rust to make memory safety guarantees without needing a garbage collector, so it’s important to understand ho...
The Rust Programming Language
Book
2024-02-01
~1 min read
doc.rust-lang.org
Control Flow The ability to run some code depending on whether a condition is true and the ability to run some code repeatedly while a condition is true are basic building blocks in most programming languages. The most common constructs tha...
The Rust Programming Language
Book
2024-02-01
~11 min read
doc.rust-lang.org
Comments All programmers strive to make their code easy to understand, but sometimes extra explanation is warranted. In these cases, programmers leave comments in their source code that the compiler will ignore but that people reading the s...
The Rust Programming Language
Book
2024-02-01
~1 min read
doc.rust-lang.org
Functions Functions are prevalent in Rust code. You’ve already seen one of the most important functions in the language: the main function, which is the entry point of many programs. You’ve also seen the fn keyword, which allows you to decl...
The Rust Programming Language
Book
2024-02-01
~6 min read