diff --git a/nostarch/appendix.md b/nostarch/appendix.md new file mode 100644 index 000000000..6f061d779 --- /dev/null +++ b/nostarch/appendix.md @@ -0,0 +1,63 @@ +# Appendix + +The following sections contain reference material you may find useful in your +Rust journey. + +## Keywords + +The following keywords are reserved by the Rust language and may not be used as +names of functions, variables, macros, modules, crates, constants, static +values, attributes, struct fields, or arguments. + +* `abstract` +* `alignof` +* `as` +* `become` +* `box` +* `break` +* `const` +* `continue` +* `crate` +* `do` +* `else` +* `enum` +* `extern` +* `false` +* `final` +* `fn` +* `for` +* `if` +* `impl` +* `in` +* `let` +* `loop` +* `macro` +* `match` +* `mod` +* `move` +* `mut` +* `offsetof` +* `override` +* `priv` +* `proc` +* `pub` +* `pure` +* `ref` +* `return` +* `Self` +* `self` +* `sizeof` +* `static` +* `struct` +* `super` +* `trait` +* `true` +* `type` +* `typeof` +* `unsafe` +* `unsized` +* `use` +* `virtual` +* `where` +* `while` +* `yield` diff --git a/nostarch/chapter01.md b/nostarch/chapter01.md new file mode 100644 index 000000000..2cbf8700d --- /dev/null +++ b/nostarch/chapter01.md @@ -0,0 +1,492 @@ +# Introduction + +Welcome to “The Rust Programming Language”, an introductory book about Rust. +Rust is a programming language that’s focused on safety, speed, and +concurrency. Its design lets you create programs that have the performance and +control of a low-level language, but with helpful abstractions that feel like a +high-level language. The Rust community welcomes all programmers who have their +experience in languages like C and are looking for a safer alternative, as well +as programmers from languages like Python who are looking for ways to write more +performant code without losing expressiveness. + +Rust provides the majority of its safety checks at compile time and without a +garbage collector so that your program's runtime isn't impacted. This makes it +useful in a number of use cases that other languages aren’t good at: embedding +in other languages, programs with specific space and time requirements, and +writing low-level code, like device drivers and operating systems. It's also +great for web applications: it powers the Rust package registry site, crates.io! +We're excited to see what _you_ create with Rust. + +This book is written for a reader who already knows how to program in at least +one programming language. After reading this book, you should be comfortable +writing Rust programs. We’ll be learning Rust through small, focused examples +that build on each other to demonstrate how to use various features of Rust as +well as how they work behind the scenes. + +## Contributing to the book + +This book is open source. If you find an error, please don’t hesitate to file an +issue or send a pull request [on GitHub]. + +[on GitHub]: https://github.com/rust-lang/book + +## Installation + +The first step to using Rust is to install it. You’ll need an internet +connection to run the commands in this chapter, as we’ll be downloading Rust +from the internet. + +We’ll be showing off a number of commands using a terminal, and those lines all +start with `$`. You don't need to type in the `$`s; they are there to indicate +the start of each command. You’ll see many tutorials and examples around the web +that follow this convention: `$` for commands run as a regular user, and `#` +for commands you should be running as an administrator. Lines that don't start +with `$` are typically showing the output of the previous command. + +### Installing on Linux or Mac + +If you're on Linux or a Mac, all you need to do is open a terminal and type +this: + +```bash +$ curl https://sh.rustup.rs -sSf | sh +``` + +This will download a script and start the installation. You may be prompted for +your password. If it all goes well, you’ll see this appear: + +```bash +Rust is installed now. Great! +``` + +### Installing on Windows + +If you're on Windows, please download the appropriate [installer][install-page]. + +[install-page]: https://www.rust-lang.org/install.html + +### Uninstalling + +Uninstalling Rust is as easy as installing it. On Linux or Mac, just run +the uninstall script: + +```bash +$ rustup self uninstall +``` + +If you used the Windows installer, you can re-run the `.msi` and it will give +you an uninstall option. + +### Troubleshooting + +If you've got Rust installed, you can open up a shell, and type this: + +```bash +$ rustc --version +``` + +You should see the version number, commit hash, and commit date in a format +similar to this for the latest stable version at the time you install: + +```bash +rustc x.y.z (abcabcabc yyyy-mm-dd) +``` + +If you see this, Rust has been installed successfully! +Congrats! + +If you don't and you're on Windows, check that Rust is in your `%PATH%` system +variable. If it isn't, run the installer again, select "Change" on the "Change, +repair, or remove installation" page and ensure "Add to PATH" is checked. + +If not, there are a number of places where you can get help. The easiest is +[the #rust IRC channel on irc.mozilla.org][irc], which you can access through +[Mibbit][mibbit]. Click that link, and you'll be chatting with other Rustaceans +(a silly nickname we call ourselves) who can help you out. Other great resources +include [the user’s forum][users] and [Stack Overflow][stackoverflow]. + +[irc]: irc://irc.mozilla.org/#rust +[mibbit]: http://chat.mibbit.com/?server=irc.mozilla.org&channel=%23rust +[users]: https://users.rust-lang.org/ +[stackoverflow]: http://stackoverflow.com/questions/tagged/rust + +### Local documentation + +The installer also includes a copy of the documentation locally, so you can +read it offline. On Linux or Mac, run `rustup doc` to open the local +documentation in your browser. On Windows, the documentation is in a +`share/doc` directory inside the directory where Rust was installed. + +## Hello, World! + +Now that you have Rust installed, let’s write your first Rust program. It's +traditional when learning a new language to write a little program to print the +text “Hello, world!” to the screen, and in this section, we'll follow that +tradition. + +> Note: This book assumes basic familiarity with the command line. Rust itself +> makes no specific demands about your editing, tooling, or where your code +> lives, so if you prefer an IDE to the command line, that's an option. + +### Creating a Project File + +First, make a file to put your Rust code in. Rust doesn't care where your code +lives, but for this book, we'd suggest making a *projects* directory in your +home directory and keeping all your projects there. Open a terminal and enter +the following commands to make a directory for this particular project: + +```bash +$ mkdir ~/projects +$ cd ~/projects +$ mkdir hello_world +$ cd hello_world +``` + +> Note: If you’re on Windows and not using PowerShell, the `~` that represents +> your home directory may not work. +> Consult the documentation for your shell for more details. + +### Writing and Running a Rust Program + +Next, make a new source file and call it *main.rs*. Rust files always end with +the *.rs* extension. If you’re using more than one word in your filename, use +an underscore to separate them. For example, you'd use *hello_world.rs* rather +than *helloworld.rs*. + +Now open the *main.rs* file you just created, and type the following code: + +```rust +fn main() { + println!("Hello, world!"); +} +``` + +Save the file, and go back to your terminal window. On Linux or OSX, enter the +following commands: + +```bash +$ rustc main.rs +$ ./main +Hello, world! +``` + +On Windows, just replace `main` with `main.exe`. Regardless of your operating +system, you should see the string `Hello, world!` print to the terminal. If you +did, then congratulations! You've officially written a Rust program. That makes +you a Rust programmer! Welcome. + +### Anatomy of a Rust Program + +Now, let’s go over what just happened in your "Hello, world!" program in +detail. Here's the first piece of the puzzle: + +```rust +fn main() { + +} +``` + +These lines define a *function* in Rust. The `main` function is special: it's +the first thing that is run for every executable Rust program. The first line +says, “I’m declaring a function named `main` that takes no arguments and +returns nothing.” If there were arguments, they would go inside the parentheses +(`(` and `)`). We aren’t returning anything from this function, so we have +omitted the return type entirely. If there was a return type, there would be a +`->` and the return type after the parentheses. + +Also note that the function body is wrapped in curly braces (`{` and `}`). Rust +requires these around all function bodies. It's considered good style to put +the opening curly brace on the same line as the function declaration, with one +space in between. + +Inside the `main()` function: + +```rust + println!("Hello, world!"); +``` + +This line does all of the work in this little program: it prints text to the +screen. There are a number of details that are important here. The first is +that it’s indented with four spaces, not tabs. + +The second important part is `println!()`. This is calling a Rust *macro*, +which is how metaprogramming is done in Rust. If it were calling a function +instead, it would look like this: `println()` (without the `!`). We'll discuss +Rust macros in more detail in Chapter XX, but for now you just need to know +that when you see a `!` that means that you’re calling a macro instead of a +normal function. + +Next is `"Hello, world!"` which is a *string*. We pass this string as an +argument to `println!()`, which prints the string to the screen. Easy enough! + +The line ends with a semicolon (`;`). The `;` indicates that this expression is +over, and the next one is ready to begin. Most lines of Rust code end with a +`;`. + +### Compiling and Running Are Separate Steps + +In "Writing and Running a Rust Program", we showed you how to run a newly +created program. We'll break that process down and examine each step now. + +Before running a Rust program, you have to compile it. You can use the Rust +compiler by entering the `rustc` command and passing it the name of your source +file, like this: + +```bash +$ rustc main.rs +``` + +If you come from a C or C++ background, you'll notice that this is similar to +`gcc` or `clang`. After compiling successfully, Rust should output a binary +executable, which you can see on Linux or OSX by entering the `ls` command in +your shell as follows: + +```bash +$ ls +main main.rs +``` + +On Windows, you'd enter: + +```bash +$ dir +main.exe main.rs +``` + +This shows we have two files: the source code, with the `.rs` extension, and the +executable (`main.exe` on Windows, `main` everywhere else). All that's left to +do from here is run the `main` or `main.exe` file, like this: + +```bash +$ ./main # or main.exe on Windows +``` + +If *main.rs* were your "Hello, world!" program, this would print `Hello, +world!` to your terminal. + +If you come from a dynamic language like Ruby, Python, or JavaScript, you may +not be used to compiling and running a program being separate steps. Rust is an +*ahead-of-time compiled* language, which means that you can compile a program, +give it to someone else, and they can run it even without having Rust +installed. If you give someone a `.rb`, `.py`, or `.js` file, on the other +hand, they need to have a Ruby, Python, or JavaScript implementation installed +(respectively), but you only need one command to both compile and run your +program. Everything is a tradeoff in language design. + +Just compiling with `rustc` is fine for simple programs, but as your project +grows, you'll want to be able to manage all of the options your project has +and make it easy to share your code with other people and projects. Next, we'll +introduce you to a tool called Cargo, which will help you write real-world Rust +programs. + +## Hello, Cargo! + +Cargo is Rust’s build system and package manager, and Rustaceans use Cargo to +manage their Rust projects because it makes a lot of tasks easier. For example, +Cargo takes care of building your code, downloading the libraries your code +depends on, and building those libraries. We call libraries your code needs +‘dependencies’ since your code depends on them. + +The simplest Rust programs, like the one we've written so far, don’t have any +dependencies, so right now, you'd only be using the part of Cargo that can take +care of building your code. As you write more complex Rust programs, you’ll +want to add dependencies, and if you start off using Cargo, that will be a lot +easier to do. + +As the vast, vast majority of Rust projects use Cargo, we will assume that +you’re using it for the rest of the book. Cargo comes installed with Rust +itself, if you used the official installers as covered in the Installation +chapter. If you installed Rust through some other means, you can check if you +have Cargo installed by typing the following into your terminal: + +```bash +$ cargo --version +``` + +If you see a version number, great! If you see an error like `command not +found`, then you should look at the documentation for the way you installed +Rust to determine how to install Cargo separately. + +### Creating a Project with Cargo + +Let's create a new project using Cargo and look at how it differs from our +project in `hello_world`. Go back to your projects directory (or wherever you +decided to put your code): + +```bash +$ cd ~/projects +``` + +And then run: + +```bash +$ cargo new hello_cargo --bin +$ cd hello_cargo +``` + +We passed the `--bin` argument to `cargo new` because our goal is to make an +executable application, as opposed to a library. Executables are often called +*binaries* (as in `/usr/bin`, if you’re on a Unix system). `hello_cargo` is the +name we've chosen for our project, and Cargo creates its files in a directory +of the same name that we can then go into. + +If we list the files in the `hello_cargo` directory, we can see that Cargo has +generated two files and one directory for us: a `Cargo.toml` and a *src* +directory with a *main.rs* file inside. It has also initialized a new `git` +repository in the `hello_cargo` directory for us; you can change this to use a +different version control system, or no version control system, by using the +`--vcs` flag. + +Open up `Cargo.toml` in your text editor of choice. It should look something +like this: + +```toml +[package] +name = "hello_cargo" +version = "0.1.0" +authors = ["Your Name "] + +[dependencies] +``` + +This file is in the *[TOML]* (Tom's Obvious, Minimal Language) format. TOML is +similar to INI but has some extra goodies and is used as Cargo’s +configuration format. + +[TOML]: https://github.com/toml-lang/toml + +The first line, `[package]`, is a section heading that indicates that the +following statements are configuring a package. As we add more information to +this file, we’ll add other sections. + +The next three lines set the three bits of configuration that Cargo needs to +see in order to know that it should compile your program: its name, what +version it is, and who wrote it. Cargo gets your name and email information +from your environment. If it’s not correct, go ahead and fix that and save the +file. + +The last line, `[dependencies]`, is the start of a section for you to list any +crates that your project will depend on so that Cargo knows to download and +compile those too. We won't need any other crates for this project, but we will +in the guessing game tutorial in the next chapter. + +Now let's look at `src/main.rs`: + +```rust +fn main() { + println!("Hello, world!"); +} +``` + +Cargo has generated a "Hello World!" for you, just like the one we wrote +earlier! So that part is the same. The differences between our previous project +and the project generated by Cargo that we've seen so far are: + +1. Our code goes in the `src` directory +2. The top level contains a `Cargo.toml` configuration file + +Cargo expects your source files to live inside the *src* directory so that the +top-level project directory is just for READMEs, license information, +configuration files, and anything else not related to your code. In this way, +using Cargo helps you keep your projects nice and tidy. There's a place for +everything, and everything is in its place. + +If you started a project that doesn't use Cargo, as we did with our project in +the `hello_world` directory, you can convert it to a project that does use +Cargo by moving your code into the `src` directory and creating an appropriate +`Cargo.toml`. + +### Building and Running a Cargo Project + +Now let's look at what's different about building and running your Hello World +program through Cargo! To do so, enter the following commands: + +```bash +$ cargo build + Compiling hello_cargo v0.1.0 (file:///home/yourname/projects/hello_cargo) +``` + +This should have created an executable file in `target/debug/hello_cargo` (or `target/debug/hello_cargo.exe` on Windows), which you can run with this command: + +```bash +$ ./target/debug/hello_cargo # or ./target/debug/hello_cargo.exe on Windows +Hello, world! +``` + +Bam! If all goes well, `Hello, world!` should print to the terminal once more. + +Running `cargo build` for the first time also causes Cargo to create a new file +at the top level called *Cargo.lock*, which looks like this: + +```toml +[root] +name = "hello_cargo" +version = "0.1.0" +``` + +Cargo uses the *Cargo.lock* file to keep track of dependencies in your +application. This project doesn't have dependencies, so the file is a bit +sparse. Realistically, you won't ever need to touch this file yourself; just +let Cargo handle it. + +We just built a project with `cargo build` and ran it with +`./target/debug/hello_cargo`, but we can actually do both in one step with +`cargo run` as follows: + +```bash +$ cargo run + Running `target/debug/hello_cargo` +Hello, world! +``` + +Notice that this time, we didn't see the output that Cargo was compiling +`hello_cargo`. Cargo figured out that the files haven’t changed, so it just ran +the binary. If you had modified your source code, Cargo would have rebuilt the +project before running it, and you would have seen something like this: + +```bash +$ cargo run + Compiling hello_cargo v0.1.0 (file:///home/yourname/projects/hello_cargo) + Running `target/debug/hello_cargo` +Hello, world! +``` + +So a few more differences we've now seen: + +3. Instead of using `rustc`, build a project using `cargo build` (or build and run it in one step with `cargo run`) +4. Instead of the result of the build being put in the same directory as our code, Cargo will put it in the `target/debug` directory. + +### Building for Release + +When your project is finally ready for release, you can use `cargo build +--release` to compile your project with optimizations. This will create an +executable in `target/release` instead of `target/debug`. These optimizations +make your Rust code run faster, but turning them on makes your program take +longer to compile. This is why there are two different profiles: one for +development when you want to be able to rebuild quickly and often, and one for +building the final program you’ll give to a user that won't be rebuilt and +that we want to run as fast as possible. If you're benchmarking the running +time of your code, be sure to run `cargo build --release` and benchmark with +the executable in `target/release`. + +### Cargo as Convention + +With simple projects, Cargo doesn't provide a whole lot of value over just +using `rustc`, but it will prove its worth as you continue. With complex +projects composed of multiple crates, it’s much easier to let Cargo coordinate +the build. With Cargo, you can just run `cargo build`, and it should work the +right way. Even though this project is simple, it now uses much of the real +tooling you’ll use for the rest of your Rust career. In fact, you can get +started with virtually all Rust projects you might find that you want to work +on with the following commands: + +```bash +$ git clone someurl.com/someproject +$ cd someproject +$ cargo build +``` + +> Note: If you want to look at Cargo in more detail, check out the official +[Cargo guide], which covers all of its features. + +[Cargo guide]: http://doc.crates.io/guide.html diff --git a/nostarch/chapter02.md b/nostarch/chapter02.md new file mode 100644 index 000000000..0deffef99 --- /dev/null +++ b/nostarch/chapter02.md @@ -0,0 +1,966 @@ +# Guessing Game + +Let's jump into Rust with a hands-on project! We’ll implement a classic +beginner programming problem: the guessing game. Here’s how it works: Our +program will generate a random integer between one and a hundred. It will then +prompt us to enter a guess. Upon entering our guess, it will tell us if we’re +too low or too high. Once we guess correctly, it will congratulate us. + +## Set up + +Let’s set up a new project. Go to your projects directory, and create a new project using Cargo. + +```bash +$ cd ~/projects +$ cargo new guessing_game --bin +$ cd guessing_game +``` + +We pass the name of our project to `cargo new`, then the `--bin` flag, since +we’re going to be making another binary like in Chapter 1. + +Take a look at the generated `Cargo.toml`: + +```toml +[package] +name = "guessing_game" +version = "0.1.0" +authors = ["Your Name "] + +[dependencies] +``` + +If the authors information that Cargo got from your environment is not correct, +go ahead and fix that. + +And as we saw in the last chapter, `cargo new` generates a ‘Hello, world!’ for +us. Check out `src/main.rs`: + +```rust +fn main() { + println!("Hello, world!"); +} +``` + +Let’s try compiling what Cargo gave us and running it in the same step, using the `cargo run` command: + +```bash +$ cargo run + Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game) + Running `target/debug/guessing_game` +Hello, world! +``` + +Great! The `run` command comes in handy when you need to rapidly iterate on a +project. Our game is such a project: we want to quickly test each +iteration before moving on to the next one. + +Now open up your `src/main.rs` again. We’ll be writing all of our code in this +file. + +## Processing a Guess + +Let’s get to it! The first thing we need to do for our guessing game is +allow our player to input a guess. Put this in your `src/main.rs`: + +```rust,ignore +use std::io; + +fn main() { + println!("Guess the number!"); + + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin().read_line(&mut guess) + .expect("Failed to read line"); + + println!("You guessed: {}", guess); +} +``` + +There’s a lot here! Let’s go over it, bit by bit. + +```rust,ignore +use std::io; +``` + +We’ll need to take user input and then print the result as output. As such, we +need the `io` library from the standard library. Rust only imports a few things +by default into every program, [the ‘prelude’][prelude]. If it’s not in the +prelude, you’ll have to `use` it directly. Using the `std::io` library gets +you a number of useful `io`-related things, so that's what we've done here. + +[prelude]: ../std/prelude/index.html + +```rust,ignore +fn main() { +``` + +As you’ve seen in Chapter 1, the `main()` function is the entry point into the +program. The `fn` syntax declares a new function, the `()`s indicate that +there are no arguments, and `{` starts the body of the function. + +```rust,ignore +println!("Guess the number!"); + +println!("Please input your guess."); +``` + +We previously learned in Chapter 1 that `println!()` is a macro that +prints a string to the screen. + +### Variable Bindings + +```rust,ignore +let mut guess = String::new(); +``` + +Now we’re getting interesting! There’s a lot going on in this little line. +The first thing to notice is that this is a let statement, which is +used to create what are called ‘variable bindings’. Here's an example: + +```rust,ignore +let foo = bar; +``` + +This will create a new binding named `foo`, and bind it to the value `bar`. In +many languages, this is called a ‘variable’, but Rust’s variable bindings have +a few tricks up their sleeves. + +For example, they’re immutable by default. That’s why our example +uses `mut`: it makes a binding mutable, rather than immutable. + +```rust +let foo = 5; // immutable. +let mut bar = 5; // mutable +``` + +Oh, and `//` will start a comment, until the end of the line. Rust ignores +everything in comments. + +So now we know that `let mut guess` will introduce a mutable binding named +`guess`, but we have to look at the other side of the `=` for what it’s +bound to: `String::new()`. + +`String` is a string type, provided by the standard library. A +[`String`][string] is a growable, UTF-8 encoded bit of text. + +[string]: ../std/string/struct.String.html + +The `::new()` syntax uses `::` because this is an ‘associated function’ of +a particular type. That is to say, it’s associated with `String` itself, +rather than a particular instance of a `String`. Some languages call this a +‘static method’. + +This function is named `new()`, because it creates a new, empty `String`. +You’ll find a `new()` function on many types, as it’s a common name for making +a new value of some kind. + +Let’s move forward: + +```rust,ignore +io::stdin().read_line(&mut guess) + .expect("Failed to read line"); +``` + +Let’s go through this together bit-by-bit. The first line has two parts. Here’s +the first: + +```rust,ignore +io::stdin() +``` + +Remember how we `use`d `std::io` on the first line of the program? We’re now +calling an associated function on it. If we didn’t `use std::io`, we could +have written this line as `std::io::stdin()`. + +This particular function returns a handle to the standard input for your +terminal. More specifically, a [std::io::Stdin][iostdin]. + +[iostdin]: ../std/io/struct.Stdin.html + +The next part will use this handle to get input from the user: + +```rust,ignore +.read_line(&mut guess) +``` + +Here, we call the [`read_line()`][read_line] method on our handle. We’re also +passing one argument to `read_line()`: `&mut guess`. + +[read_line]: ../std/io/struct.Stdin.html#method.read_line + +Remember how we bound `guess` above? We said it was mutable. However, +`read_line` doesn’t take a `String` as an argument: it takes a `&mut String`. +The `&` is the feature of Rust called a ‘reference’, which allows you to have +multiple ways to access one piece of data in order to reduce copying. +References are a complex feature, as one of Rust’s major selling points is how +safe and easy it is to use references. We don’t need to know a lot of those +details to finish our program right now, though; Chapter XX will cover them in +more detail. For now, all we need to know is that like `let` bindings, +references are immutable by default. Hence, we need to write `&mut guess`, +rather than `&guess`. + +Why does `read_line()` take a mutable reference to a string? Its job is +to take what the user types into standard input and place that into a +string. So it takes that string as an argument, and in order to add +the input, that string needs to be mutable. + +But we’re not quite done with this line of code, though. While it’s +a single line of text, it’s only the first part of the single logical line of +code. This is the second part of the line: + +```rust,ignore +.expect("Failed to read line"); +``` + +When you call a method with the `.foo()` syntax, you may introduce a newline +and other whitespace. This helps you split up long lines. We _could_ have +written this code as: + +```rust,ignore +io::stdin().read_line(&mut guess).expect("failed to read line"); +``` + +But that gets hard to read. So we’ve split it up, two lines for two method +calls. + +### The `Result` Type + +We already talked about `read_line()`, but what about `expect()`? Well, +we already mentioned that `read_line()` puts what the user types into the `&mut +String` we pass it. But it also returns a value: in this case, an +[`io::Result`][ioresult]. Rust has a number of types named `Result` in its +standard library: a generic [`Result`][result], and then specific versions for +sub-libraries, like `io::Result`. + +[ioresult]: ../std/io/type.Result.html +[result]: ../std/result/enum.Result.html + +The purpose of these `Result` types is to encode error handling information. +Values of the `Result` type, like any type, have methods defined on them. In +this case, `io::Result` has an [`expect()` method][expect] that takes a value +it’s called on, and if it isn’t a successful result, will cause our program to +crash and display the message that we passed as an argument to `expect()`. + +[expect]: ../std/result/enum.Result.html#method.expect + +If we don't call this method, our program will compile, but we’ll get a warning: + +```bash +$ cargo build + Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game) +src/main.rs:10:5: 10:39 warning: unused result which must be used, +#[warn(unused_must_use)] on by default +src/main.rs:10 io::stdin().read_line(&mut guess); + ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +``` + +Rust warns us that we haven’t used the `Result` value. This warning comes from +a special annotation that `io::Result` has. Rust is trying to tell you that you +haven’t handled a possible error. The right way to suppress the error is to +actually write error handling. Luckily, if we want to crash if there’s a +problem, we can use `expect()`. If we can recover from the error somehow, we’d +do something else, but we’ll save that for a future project. + +### `println!()` Placeholders + +There’s only one line of this first example left, aside from the closing curly +brace: + +```rust,ignore + println!("You guessed: {}", guess); +} +``` + +This prints out the string we saved our input in. The `{}`s are a placeholder: +think of `{}` as little crab pincers, holding a value in place. The first `{}` +holds the first value after the format string, the second set holds the second +value, and so on. Printing out multiple values in one call to `println!()` would then look like this: + +```rust +let x = 5; +let y = 10; + +println!("x and y: {} and {}", x, y); +``` + +Which would print out "x and y: 5 and 10". + +Anyway, back to our guessing game. We can run what we have with `cargo run`: + +```bash +$ cargo run + Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game) + Running `target/debug/guessing_game` +Guess the number! +Please input your guess. +6 +You guessed: 6 +``` + +All right! Our first part is done: we can get input from the keyboard and then +print it back out. + +## Generating a secret number + +Next, we need to generate a secret number. Rust does not yet include random +number functionality in its standard library. The Rust team does, however, +provide a [`rand` crate][randcrate]. A ‘crate’ is a package of Rust code. +We’ve been building a ‘binary crate’, which is an executable. `rand` is a +‘library crate’, which contains code that’s intended to be used with other +programs. + +[randcrate]: https://crates.io/crates/rand + +Using external crates is where Cargo really shines. Before we can write +the code using `rand`, we need to modify our `Cargo.toml`. Open it up, and +add this line at the bottom beneath the `[dependencies]` section header that +Cargo created for you: + +```toml +[dependencies] + +rand = "0.3.14" +``` + +The `[dependencies]` section of `Cargo.toml` is like the `[package]` section: +everything that follows the section heading is part of that section, until +another section starts. Cargo uses the dependencies section to know what +dependencies on external crates you have and what versions of those crates you +require. In this case, we’ve specified the `rand` crate with the semantic +version specifier `0.3.14`. Cargo understands [Semantic Versioning][semver], a +standard for writing version numbers. A bare number like above is actually +shorthand for `^0.3.14`, which means "any version that has a public API +compatible with version 0.3.14". + +[semver]: http://semver.org + +Now, without changing any of our code, let’s build our project: + +```bash +$ cargo build + Updating registry `https://github.com/rust-lang/crates.io-index` + Downloading rand v0.3.14 + Downloading libc v0.2.14 + Compiling libc v0.2.14 + Compiling rand v0.3.14 + Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game) +``` + +You may see different versions (but they will be compatible, thanks to semver!) +and the lines may be in a different order. + +Lots of new output! Now that we have an external dependency, Cargo fetches the +latest versions of everything from the *registry*, which is a copy of data from +[Crates.io][cratesio]. Crates.io is where people in the Rust ecosystem +post their open source Rust projects for others to use. + +[cratesio]: https://crates.io + +After updating the registry, Cargo checks our `[dependencies]` and downloads +any we don’t have yet. In this case, while we only said we wanted to depend on +`rand`, we’ve also grabbed a copy of `libc`. This is because `rand` depends on +`libc` to work. After downloading them, it compiles them and then compiles +our project. + +If we run `cargo build` again, we’ll get different output: + +```bash +$ cargo build +``` + +That’s right, no output! Cargo knows that our project has been built, that +all of its dependencies are built, and that no changes have been made. There’s +no reason to do all that stuff again. With nothing to do, it simply +exits. If we open up `src/main.rs`, make a trivial change, then save it again, +we’ll only see one line: + +```bash +$ cargo build + Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game) +``` + +What happens when next week version `v0.3.15` of the `rand` crate comes out, +with an important bugfix? While getting bugfixes is important, what if `0.3.15` +contains a regression that breaks our code? + +The answer to this problem is the `Cargo.lock` file created the first time we +ran `cargo build` that is now in your project directory. When you build your +project for the first time, Cargo figures out all of the versions that fit your +criteria then writes them to the `Cargo.lock` file. When you build your project +in the future, Cargo will see that the `Cargo.lock` file exists and then use +that specific version rather than do all the work of figuring out versions +again. This lets you have a repeatable build automatically. In other words, +we’ll stay at `0.3.14` until we explicitly upgrade, and so will anyone who we +share our code with, thanks to the lock file. + +What about when we _do_ want to use `v0.3.15`? Cargo has another command, +`update`, which says ‘ignore the `Cargo.lock` file and figure out all the +latest versions that fit what we’ve specified in `Cargo.toml`. If that works, +write those versions out to the lock file’. But by default, Cargo will only +look for versions larger than `0.3.0` and smaller than `0.4.0`. If we want to +move to `0.4.x`, we’d have to update what is in the `Cargo.toml` file. When we +do, the next time we `cargo build`, Cargo will update the index and re-evaluate +our `rand` requirements. + +There’s a lot more to say about [Cargo][doccargo] and [its +ecosystem][doccratesio] that we will get into in Chapter XX, but for now, +that’s all we need to know. Cargo makes it really easy to re-use libraries, so +Rustaceans are able to write smaller projects which are assembled out of a +number of sub-packages. + +[doccargo]: http://doc.crates.io +[doccratesio]: http://doc.crates.io/crates-io.html + +Let’s get on to actually _using_ `rand`. Here’s our next step: + +```rust,ignore +extern crate rand; + +use std::io; +use rand::Rng; + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1, 101); + + println!("The secret number is: {}", secret_number); + + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin().read_line(&mut guess) + .expect("failed to read line"); + + println!("You guessed: {}", guess); +} +``` + +The first thing we’ve done is change the first line. It now says `extern crate +rand`. Because we declared `rand` in our `[dependencies]`, we can now put +`extern crate` in our code to let Rust know we’ll be making use of that +dependency. This also does the equivalent of a `use rand;` as well, so we can +call anything in the `rand` crate by prefixing it with `rand::`. + +Next, we added another `use` line: `use rand::Rng`. We’re going to use a +method in a moment, and it requires that `Rng` be in scope to work. The basic +idea is this: methods are defined on something called ‘traits’, and for the +method to work, it needs the trait to be in scope. For more about the +details, read the traits section in Chapter XX. + +There are two other lines we added, in the middle: + +```rust,ignore +let secret_number = rand::thread_rng().gen_range(1, 101); + +println!("The secret number is: {}", secret_number); +``` + +We use the `rand::thread_rng()` function to get a copy of the random number +generator, which is local to the particular thread of execution +we’re in. Because we put `use rand::Rng` above, the random number generator has +a `gen_range()` method available. This method takes two numbers as arguments +and generates a random number between them. It’s inclusive on the lower bound +but exclusive on the upper bound, so we need `1` and `101` to ask for a number +ranging from one to a hundred. + +The second line prints out the secret number. This is useful while +we’re developing our program to let us easily test it out, but we’ll be +deleting it for the final version. It’s not much of a game if it prints out +the answer when you start it up! + +Try running our new program a few times: + +```bash +$ cargo run + Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game) + Running `target/debug/guessing_game` +Guess the number! +The secret number is: 7 +Please input your guess. +4 +You guessed: 4 +$ cargo run + Running `target/debug/guessing_game` +Guess the number! +The secret number is: 83 +Please input your guess. +5 +You guessed: 5 +``` + +You should get different random numbers, and they should all be between 1 and +100. Great job! Next up: comparing our guess to the secret number. + +## Comparing guesses + +Now that we’ve got user input, let’s compare our guess to the secret number. +Here’s part of our next step. It won't quite compile yet though: + +```rust,ignore +extern crate rand; + +use std::io; +use std::cmp::Ordering; +use rand::Rng; + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1, 101); + + println!("The secret number is: {}", secret_number); + + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin().read_line(&mut guess) + .expect("failed to read line"); + + println!("You guessed: {}", guess); + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => println!("You win!"), + } +} +``` + +A few new bits here. The first is another `use`. We bring a type called +`std::cmp::Ordering` into scope. Then we add five new lines at the bottom that +use that type: + +```rust,ignore +match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => println!("You win!"), +} +``` + +The `cmp()` method can be called on anything that can be compared, and it +takes a reference to the thing you want to compare it to. It returns the +`Ordering` type we `use`d earlier. We use a [`match`][match] statement to +determine exactly what kind of `Ordering` it is. `Ordering` is an +[`enum`][enum], short for ‘enumeration’, which looks like this: + +```rust +enum Foo { + Bar, + Baz, +} +``` + +[match]: match.html +[enum]: enums.html + +With this definition, anything of type `Foo` can be either a +`Foo::Bar` or a `Foo::Baz`. We use the `::` to indicate the +namespace for a particular `enum` variant. + +The [`Ordering`][ordering] `enum` has three possible variants: `Less`, `Equal`, +and `Greater`. The `match` statement takes a value of a type and lets you +create an ‘arm’ for each possible value. An arm is made up of a pattern and the +code that we should execute if the pattern matches the value of the type. Since +we have three types of `Ordering`, we have three arms: + +```rust,ignore +match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => println!("You win!"), +} +``` + +[ordering]: ../std/cmp/enum.Ordering.html + +If it’s `Less`, we print `Too small!`, if it’s `Greater`, `Too big!`, and if +`Equal`, `You win!`. `match` is really useful and is used often in Rust. + +We did mention that this won’t quite compile yet, though. Let’s try it: + +```bash +$ cargo build + Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game) +src/main.rs:23:21: 23:35 error: mismatched types [E0308] +src/main.rs:23 match guess.cmp(&secret_number) { + ^~~~~~~~~~~~~~ +src/main.rs:23:21: 23:35 help: run `rustc --explain E0308` to see a detailed explanation +src/main.rs:23:21: 23:35 note: expected type `&std::string::String` +src/main.rs:23:21: 23:35 note: found type `&_` +error: aborting due to previous error +Could not compile `guessing_game`. +``` + +Whew! This is a big error. The core of it is that we have ‘mismatched types’. +Rust has a strong, static type system. However, it also has type inference. +When we wrote `let guess = String::new()`, Rust was able to infer that `guess` +should be a `String`, so it doesn’t make us write out the type. With our +`secret_number`, there are a number of types which can have a value between one +and a hundred: `i32`, a thirty-two-bit number, or `u32`, an unsigned +thirty-two-bit number, or `i64`, a sixty-four-bit number or others. So far, +that hasn’t mattered, and so Rust defaults to an `i32`. However, here, Rust +doesn’t know how to compare the `guess` and the `secret_number`. They need to +be the same type. + +Ultimately, we want to convert the `String` we read as input +into a real number type so that we can compare it to the guess numerically. We +can do that with two more lines. Here’s our new program: + +```rust,ignore +extern crate rand; + +use std::io; +use std::cmp::Ordering; +use rand::Rng; + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1, 101); + + println!("The secret number is: {}", secret_number); + + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin().read_line(&mut guess) + .expect("failed to read line"); + + let guess: u32 = guess.trim().parse() + .expect("Please type a number!"); + + println!("You guessed: {}", guess); + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => println!("You win!"), + } +} +``` + +The new two lines: + +```rust,ignore +let guess: u32 = guess.trim().parse() + .expect("Please type a number!"); +``` + +Wait a minute, didn't we already have a `guess`? We do, but Rust allows us +to ‘shadow’ the previous `guess` with a new one. This is often used in this +exact situation, where `guess` starts as a `String`, but we want to convert it +to a `u32`. Shadowing lets us re-use the `guess` name rather than forcing us +to come up with two unique names like `guess_str` and `guess` or something +else. + +We bind `guess` to an expression that looks like something we wrote earlier: + +```rust,ignore +guess.trim().parse() +``` + +Here, `guess` refers to the old `guess`, the one that was a `String` with our +input in it. The `trim()` method on `String`s will eliminate any white space at +the beginning and end of our string. This is important, as we had to press the +‘return’ key to satisfy `read_line()`. If we type `5` and hit return, `guess` +looks like this: `5\n`. The `\n` represents ‘newline’, the enter key. `trim()` +gets rid of this, leaving our string with only the `5`. + +The [`parse()` method on strings][parse] parses a string into some kind of +number. Since it can parse a variety of numbers, we need to give Rust a hint as +to the exact type of number we want. Hence, `let guess: u32`. The colon (`:`) +after `guess` tells Rust we’re going to annotate its type. `u32` is an +unsigned, thirty-two bit integer. Rust has a number of built-in number +types, but we’ve chosen `u32`. It’s a good default choice for a small +positive number. You'll see the other number types in Chapter XX. + +[parse]: ../std/primitive.str.html#method.parse + +Just like `read_line()`, our call to `parse()` could cause an error. What if +our string contained `A👍%`? There’d be no way to convert that to a number. As +such, we’ll do the same thing we did with `read_line()`: use the `expect()` +method to crash if there’s an error. + +Let’s try our program out! + +```bash +$ cargo run + Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game) + Running `target/guessing_game` +Guess the number! +The secret number is: 58 +Please input your guess. + 76 +You guessed: 76 +Too big! +``` + +Nice! You can see we even added spaces before our guess, and it still figured +out that we guessed 76. Run the program a few times. Verify that guessing +the secret number works, as well as guessing a number too small. + +Now we’ve got most of the game working, but we can only make one guess. Let’s +change that by adding loops! + +## Looping + +The `loop` keyword gives us an infinite loop. Let’s add that in: + +```rust,ignore +extern crate rand; + +use std::io; +use std::cmp::Ordering; +use rand::Rng; + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1, 101); + + println!("The secret number is: {}", secret_number); + + loop { + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin().read_line(&mut guess) + .expect("failed to read line"); + + let guess: u32 = guess.trim().parse() + .expect("Please type a number!"); + + println!("You guessed: {}", guess); + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => println!("You win!"), + } + } +} +``` + +And try it out. But wait, didn’t we just add an infinite loop? Yup. Remember +our discussion about `parse()`? If we give a non-number answer, the program +will crash and, therefore, quit. Observe: + +```bash +$ cargo run + Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game) + Running `target/guessing_game` +Guess the number! +The secret number is: 59 +Please input your guess. +45 +You guessed: 45 +Too small! +Please input your guess. +60 +You guessed: 60 +Too big! +Please input your guess. +59 +You guessed: 59 +You win! +Please input your guess. +quit +thread 'main' panicked at 'Please type a number!: ParseIntError { kind: InvalidDigit }', src/libcore/result.rs:785 +note: Run with `RUST_BACKTRACE=1` for a backtrace. +error: Process didn't exit successfully: `target/debug/guess` (exit code: 101) +``` + +Ha! `quit` actually quits. As does any other non-number input. Well, this is +suboptimal to say the least. First, let’s actually quit when you win the game: + +```rust,ignore +extern crate rand; + +use std::io; +use std::cmp::Ordering; +use rand::Rng; + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1, 101); + + println!("The secret number is: {}", secret_number); + + loop { + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin().read_line(&mut guess) + .expect("failed to read line"); + + let guess: u32 = guess.trim().parse() + .expect("Please type a number!"); + + println!("You guessed: {}", guess); + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => { + println!("You win!"); + break; + } + } + } +} +``` + +By adding the `break` line after the `You win!`, we’ll exit the loop when we +win. Exiting the loop also means exiting the program, since the loop is the last +thing in `main()`. We have another tweak to make: when someone inputs a +non-number, we don’t want to quit, we want to ignore it. We can do that +like this: + +```rust,ignore +extern crate rand; + +use std::io; +use std::cmp::Ordering; +use rand::Rng; + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1, 101); + + println!("The secret number is: {}", secret_number); + + loop { + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin().read_line(&mut guess) + .expect("failed to read line"); + + let guess: u32 = match guess.trim().parse() { + Ok(num) => num, + Err(_) => continue, + }; + + println!("You guessed: {}", guess); + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => { + println!("You win!"); + break; + } + } + } +} +``` + +These are the lines that changed: + +```rust,ignore +let guess: u32 = match guess.trim().parse() { + Ok(num) => num, + Err(_) => continue, +}; +``` + +This is how you generally move from ‘crash on error’ to ‘actually handle the +error’: by switching from `expect()` to a `match` statement. A `Result` is the +return type of `parse()`. `Result` is an `enum` like `Ordering`, but in this +case, each variant has some data associated with it. `Ok` is a success, and +`Err` is a failure. Each contains more information: in this case, the +successfully parsed integer or an error type, respectively. When we `match` an +`Ok(num)`, that pattern sets the name `num` to the value inside the `Ok` (the +integer), and the code we run just returns that integer. In the `Err` case, we +don’t care what kind of error it is, so we just use the catch-all `_` instead +of a name. So for all errors, we run the code `continue`, which lets us move to +the next iteration of the loop, effectively ignoring the errors. + +Now we should be good! Let’s try it: + +```bash +$ cargo run + Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game) + Running `target/guessing_game` +Guess the number! +The secret number is: 61 +Please input your guess. +10 +You guessed: 10 +Too small! +Please input your guess. +99 +You guessed: 99 +Too big! +Please input your guess. +foo +Please input your guess. +61 +You guessed: 61 +You win! +``` + +Awesome! With one tiny last tweak, we can finish the guessing game. Can you +think of what it is? That’s right, we don’t want to print out the secret +number. It was good for testing, but it kind of ruins the game. Here’s our +final source: + +```rust,ignore +extern crate rand; + +use std::io; +use std::cmp::Ordering; +use rand::Rng; + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1, 101); + + loop { + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin().read_line(&mut guess) + .expect("failed to read line"); + + let guess: u32 = match guess.trim().parse() { + Ok(num) => num, + Err(_) => continue, + }; + + println!("You guessed: {}", guess); + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => { + println!("You win!"); + break; + } + } + } +} +``` + +## Complete! + +This project showed you a lot: `let`, `match`, methods, associated +functions, using external crates, and more. + +At this point, you have successfully built the Guessing Game! Congratulations! diff --git a/nostarch/chapter03.md b/nostarch/chapter03.md new file mode 100644 index 000000000..9fab1ab0f --- /dev/null +++ b/nostarch/chapter03.md @@ -0,0 +1,1303 @@ +# Common Programming Concepts in Rust + +Let's look at concepts that appear in almost every programming language and see +how they work in Rust. Many programming languages have much in common at their +core. None of the concepts presented in this chapter are unique to Rust, but +we’ll discuss Rust’s particular syntax and conventions concerning these common +concepts. + +Specifically, we’ll be talking about variable bindings, basic types, functions, +comments, and control flow. These foundations will be in every Rust +program, and learning them early will give you a strong core to start from. + +PROD: START BOX + +Keep in mind as we get into variables and functions that the Rust language has +a set of *keywords* that have been reserved for use by the language only, much +like other languages do. This means you cannot use these words as names of +variables or functions, for example. Most of these have special meaning and we +will be using them to do various things in our Rust programs; a few have no +current functionality associated but have been reserved for functionality that +might be in the Rust language in the future. You can find a list of the +keywords in Appendix XX. + +PROD: END BOX + +## Variable Bindings and Mutability + +We mentioned in Chapter XX that by default, variable bindings are *immutable*. +This is one of many nudges that Rust's design has to encourage us to write our +code to get the most of the safety and easy concurrency that Rust has to offer. +We still have the option to make our bindings mutable, though. Let's explore +how and why Rust encourages us to favor immutability, and why we might want to +opt out of that. + +Variable bindings being immutable means that once a value is bound, you can't +change that value. To illustrate this, let's generate a new project with Cargo. +Open a terminal, and navigate to the directory you want to store your projects +in. From there, run these commands: + +```bash +$ cargo new --bin bindings +$ cd bindings +``` + +Then open *src/main.rs* and replace its code with the following: + +```rust,ignore +fn main() { + let x = 5; + println!("The value of x is: {}", x); + x = 6; + println!("The value of x is: {}", x); +} +``` + +Save and run the program using `cargo run`, and you should receive an error +message, as in this output: + +```bash +$ cargo run + Compiling bindings v0.0.1 (file:///projects/bindings) +error: re-assignment of immutable variable `x` [--explain E0384] + --> src/main.rs:4:5 +4 |> x = 6; + |> ^^^^^ +note: prior assignment occurs here + --> src/main.rs:2:9 +2 |> let x = 5; + |> ^ +``` + +This is our first example of the compiler helping us find an error in our +program! Compiler errors can be frustrating. Keep in mind that they only mean +your program isn't safely doing what you want it to do yet; they do _not_ mean +that you're not a good programmer! Experienced Rustaceans still get compiler +errors. Try to keep in mind that the Rust compiler is trying to help your +program be the very best. + +PROD: START BOX +######Extended Error Explanations + +Now that you've seen an example of a Rust error, let's look at one particularly +useful aspect of errors. Rust encourages you to seek further information on the +kind of error you've received with output like this: + +```bash +error: re-assignment of immutable variable `x` [--explain E0384] +``` + +This tells us that if we pass the `--explain` flag to `rustc` with the provided +error code, we can see an extended explanation which will try to explain common +causes of and solutions to that kind of error. Not every error has a longer +explanation, but many do. Here’s the explanation for the `E0384` error we +received: + +````bash +$ rustc --explain E0384 +This error occurs when an attempt is made to reassign an immutable variable. +For example: + +``` +fn main(){ + let x = 3; + x = 5; // error, reassignment of immutable variable +} +``` + +By default, variables in Rust are immutable. To fix this error, add the keyword +`mut` after the keyword `let` when declaring the variable. For example: + +``` +fn main(){ + let mut x = 3; + x = 5; +} +``` +```` + +These explanations can really help if you’re stuck on an error, so don't +hesitate to look up the error code. The compiler is your friend, and it's there +to help. + +PROD: END BOX + +The error includes the message `re-assigment of immutable variable` because the +program tried to assign a second value to the `x` variable. + +Getting compile-time errors when your code attempts to change a value that it +previously said was immutable is important because this very situation can lead +to bugs. If one part of your code operates on an assumption that a value it's +operating on will never change, and another part of your code changes that +value, it's possible that the first code won't do what it was designed to do. +Especially when the second piece of code only changes the value _sometimes_, +this cause of bugs can be difficult to track down after the fact. + +In Rust, our code can know that a value our code assumes won't change really +won't change, because the compiler is enforcing that guarantee for us. When +reading and writing code, we don't have to keep track in our head how and where +a value might change. This can make code easier to reason about. + +Mutability is really useful, though! Bindings are immutable only by default; +you can make them mutable by adding `mut` in front of the variable name. In +addition to telling the compiler it should allow this value to be changed, it +conveys intent to future readers of the code and says that other parts of the +code will be changing this value. + +For example, change the program you just wrote to the following: + +```rust +fn main() { + let mut x = 5; + println!("The value of x is: {}", x); + x = 6; + println!("The value of x is: {}", x); +} +``` + +Running this, we get: + +```bash +$ cargo run + Compiling bindings v0.1.0 (file:///projects/bindings) + Running `target/debug/bindings` +The value of x is: 5 +The value of x is: 6 +``` + +Using `mut`, we are allowed to change the value that `x` binds to from `5` to +`6`. You might want to make a binding mutable because it makes the code easier +to understand than an implementation that only uses immutable bindings. In +cases where you're using large data structures, mutating an instance in place +may be faster than copying and returning newly allocated instances. It all +depends on the tradeoffs you want to make in your situation. + +### Shadowing + +As we saw in the guessing game tutorial, we can declare new bindings with the +same name as a previous binding, and the new binding *shadows* the previous +binding. We say that the first binding is ‘shadowed’ by the second, which means +that the second binding's value is what you will see when you use the variable +after the second binding. This can be useful if you’d like to perform a few +transformations on a value, but have the binding be immutable after those +transformations have been completed. For example: + +```rust +fn main() { + let x = 5; + + let x = x + 1; + + let x = x * 2; + + println!("The value of x is: {}", x); +} +``` + +This program first binds `x` to a value of `5`. Then, it shadows `x` by saying +`let x =` again, taking the original value and adding `1` so that the value of +`x` is then `6`. The third `let` statement also shadows `x`, taking the +previous value and multiplying it by `2` to give `x` a final value of `12`. If +you run this, it will output: + +```bash +$ cargo run + Compiling bindings v0.1.0 (file:///projects/bindings) + Running `target/debug/bindings` +The value of x is: 12 +``` + +Shadowing is useful because it lets us modify `x` without having to make the +binding mutable. This means the compiler will still keep us from accidentally +trying to mutate `x` directly later. + +Now let's look at some of the types of values that we can bind variables to. + +## Data Types + +Every value in Rust is of a certain *type*, which tells Rust what kind of data +is being given so it knows how to work with that data. In this section, we'll +look at a number of types built into the language itself split into two subsets +of Rust data types: scalar and compound. + +Something to keep in mind throughout this section: Rust is a *statically typed* +language, which means that it must know the types of all bindings at compile +time. The compiler can usually infer what type we want to use based on the +value and how we use it. When many types are possible, such as when we +converted a `String` to a numeric type using `parse()` in the guessing game +tutorial, we can add a type annotation, like this: + +```rust,ignore +let x: i32 = 5; +``` + +You will see some type annotations as we discuss the various data types. + +### Scalar Types + +A *scalar* type is one that represents a single value. There are four key +scalar types in Rust: integers, floating point numbers, booleans, and +characters. You'll likely recognize these from other programming languages, but +let's jump into how they work in Rust. + +#### Integer Types + +An *integer* is a number without a fractional component. We've used one integer +type already in this chapter, the `i32` type. This type declaration indicates +that the value it's associated with should be a signed integer (hence the `i`, +as opposed to a `u` for unsigned) for a 32-bit system. There are a number of +built-in integer types in Rust, shown in Table 3-1. + +| Length | signed | unsigned | +|--------|--------|----------| +| 8-bit | i8 | u8 | +| 16-bit | i16 | u16 | +| 32-bit | i32 | u32 | +| 64-bit | i64 | u64 | +| arch | isize | usize | + +*Table 4-1: Integer types in Rust. Each code (for example, i32) can be used to +declare the type of a value.* + +Each variant can be either signed or unsigned and has an explicit size. Signed +and unsigned merely refers to whether it is possible for the number to be +either negative or positive, meaning the number needs to have a sign with it +("signed"), or whether it will only ever be positive and can therefore be +represented without a sign ("unsigned"). It's like writing numbers on paper: +when the sign matters, a number is shown with a plus sign or minus sign, but +when it's safe to assume the number is positive, it's shown with no sign. +Signed numbers are stored using two’s complement representation (if you're +unsure what this is you can search for it online; an explanation is outside the +scope of this text). + +Finally, the `isize` and `usize` types depend on the kind of computer your +program is running on: 64-bits if you're on a 64-bit architecture, and 32-bits +if you’re on a 32-bit architecture. + +So how do you know which type of integer to use? If you're unsure, Rust's +defaults are generally good choices, and integer types default to `i32`: it’s +generally the fastest, even on 64-bit systems. The primary situation in which +you'd need to specify `isize` or `usize` is when indexing some sort of +collection, which we'll talk about in the "Arrays" section. + +#### Floating-Point Types + +Rust also has two primitive types for *floating-point numbers*, which are +numbers with decimal points. Rust's floating-point types are `f32` and `f64`, +which are 32 bits and 64 bits in size, respectively. The default type is `f64`, +as it’s roughly the same speed as `f32`, but has a larger precision. It is +possible to use an `f64` on 32 bit systems, but it will be slower than using an +`f32` on those systems. Most of the time, trading potential worse performance +for better precision is a reasonable initial choice, and you should benchmark +your code if you suspect floating-point size is a problem in your case. + +Here's an example showing floating-point numbers in action: + +```rust +fn main() { + let x = 2.0; // f64 + + let y: f32 = 3.0; // f32 +} +``` + +Floating-point numbers are represented according to the IEEE-754 standard. The +`f32` type is a single-precision float, while `f64` has double-precision. + +#### Numeric Operations + +Rust supports the usual basic mathematic operations you’d expect for all of +these number types: addition, subtraction, multiplication, division, and +modulo. This code shows how you'd use each one in a `let` statement: + +```rust +fn main() { + // addition + let sum = 5 + 10; + + // subtraction + let difference = 95.5 - 4.3; + + // multiplication + let product = 4 * 30; + + // division + let quotient = 56.7 / 32.2; + + // modulo + let remainder = 43 % 5; +} +``` + +Each expression in these statements uses a mathematical operator and evaluates +to a single value, which is then bound to a variable. + +#### The Boolean Type + +As in most other programming languages, a boolean type in Rust has two possible +values: `true` and `false`. The boolean type in Rust is specified with `bool`. +For example: + +```rust +fn main() { + let t = true; + + let f: bool = false; // with explicit type annotation +} +``` + +The main way to consume boolean values is through conditionals like an `if` +statement. We’ll cover how `if` statements work in Rust in the "Control Flow" +section of this chapter. + +#### The Character Type + +So far we’ve only worked with numbers, but Rust supports letters too. Rust’s +`char` type is the language's most primitive alphabetic type, and this code +shows one way to use it: + +```rust +fn main() { + let c = 'z'; + let z = 'ℤ'; + let heart_eyed_cat = '😻'; +} +``` + +Rust’s `char` represents a Unicode Scalar Value, which means that it can +represent a lot more than just ASCII. Accented letters, Chinese/Japanese/Korean +ideographs, emoji, and zero width spaces are all valid `char`s in Rust. Unicode +Scalar Values range from `U+0000` to `U+D7FF` and `U+E000` to `U+10FFFF` +inclusive. A "character" isn’t really a concept in Unicode, however, so your +human intuition for what a "character" is may not match up with what a `char` +is in Rust. It also means that `char`s are four bytes each. You can learn more +about Unicode Scalar Values at +*http://www.unicode.org/glossary/#unicode_scalar_value* and find a chart for +all unicode code points at *http://www.unicode.org/charts/*. + +### Compound Types + +*Compound types* can group multiple values of other types into one type. Rust +has two primitive compound types: tuples and arrays. + +#### Grouping Values into Tuples + +We’ve seen tuples already, when binding multiple values at once. A tuple is a +general way of grouping together some number of other values with distinct +types into one compound type. + +We create a tuple by writing a comma-separated list of values inside +parentheses. Each position in the tuple has a distinct type, as in this example: + +```rust +fn main() { + let tup: (i32, f64, u8) = (500, 6.4, 1); +} +``` + +Note that, unlike the examples of multiple bindings, here we bind the single +name `tup` to the entire tuple, emphasizing the fact that a tuple is considered +a single compound element. We could then use pattern matching to destructure +this tuple value, like this: + +```rust +fn main() { + let tup: (i32, f64, u8) = (500, 6.4, 1); + + let (x, y, z) = tup; + + println!("The value of y is: {}", y); +} +``` + +In this program, we first create a tuple and bind it to the name `tup`. We then +use a pattern with `let` to take `tup` and turn it into three separate +bindings, `x`, `y`, and `z`. This is called ‘destructuring’, because it breaks +the single tuple into three parts. + +Finally, we print the value of `y`, which is `6.4`. + +#### Tuple Indexing + +In addition to destructuring through pattern matching, we can also access a +tuple element directly by using a period (`.`) followed by the index of the +value we want to access. For example: + +```rust +fn main() { + let x: (i32, f64, u8) = (500, 6.4, 1); + + let five_hundred = x.0; + + let six_point_four = x.1; + + let one = x.2; +} +``` + +This program creates a tuple, `x`, and then makes new bindings to each element +by using their index. As with most programming languages, the first index in a +tuple is 0. + +### Arrays + +Another way to bind a name to a collection of multiple values is with an +*array*. Unlike a tuple, every element of an array must have the same type. +Arrays in Rust are different than arrays in some other languages because arrays +in Rust have a fixed length-- once declared, they cannot grow or shrink in size. + +In Rust, the values going into an array are written as a comma separated list +inside square brackets: + +```rust +fn main() { + let a = [1, 2, 3, 4, 5]; +} +``` + +While arrays can be useful since they are a primitive type, they aren't as +flexible as the `Vec` (short for "vector"), a similar collection type provided +by the standard library that _is_ allowed to grow or shrink in size. If you're +unsure whether to use an array or a `Vec`, you should probably go with a `Vec`, +and we'll discuss them in more detail in chapter XX. + +#### Accessing Array Elements + +An array is a single chunk of memory, allocated on the stack. We can access +elements of an array using indexing, like this: + +```rust +fn main() { + let a = [1, 2, 3, 4, 5]; + + let first = a[0]; + let second = a[1]; +} +``` + +In this example, the `first` variable will bind to `1` at index `[0]` in the +array, and `second` will bind to `2` at index `[1]` in the array. Note that +these values are copied out of the array and into `first` and `second` when the +`let` statement is called. That means if the array changes after the `let` +statements, these bindings will not, and the two variables should retain their +values. + +#### Invalid array element access + +What happens if you try to access an element of an array past the end of the +array? Say we changed our program to: + +```rust,ignore +fn main() { + let a = [1, 2, 3, 4, 5]; + + let element = a[10]; + + println!("The value of element is: {}", element); +} +``` + +Running this code with `cargo run` produces: + +```bash +$ cargo run + Compiling arrays v0.1.0 (file:///projects/arrays) + Running `target/debug/arrays` +thread '
' panicked at 'index out of bounds: the len is 5 but the index is 10', src/main.rs:4 +note: Run with `RUST_BACKTRACE=1` for a backtrace. +error: Process didn't exit successfully: `target/debug/arrays` (exit code: 101) +``` + +We can see that compiling did not give us any errors, but we got a *runtime* +error and our program didn't exit successfully. When we attempt to access an +element using indexing, Rust will check that the index we've specified is less +than the array length. If the index is greater than the length, it will +"panic", which is what it's called when a Rust program exits with an error. + +This is our first example of Rust’s safety principles in action. In many +low-level languages, this kind of check is not done, and when you provide an +incorrect index, invalid memory can be accessed. Rust protects us against this +kind of error by immediately exiting instead of allowing the memory access and +continuing. We'll discuss more of Rust’s error handling in Chapter XX. + +## How Functions Work + +Functions are pervasive in Rust code. We’ve already seen one of the most +important functions in the language: the `main()` function that’s the entry +point of many programs. We've also seen the `fn` keyword, which allows us to +declare new functions. + +Rust code uses *snake case* as the conventional style for function names. In +snake case, all letters are lower case, and there are underscores separating +words. (Rust also uses snake case for the names of variable bindings; we just +haven't used any variable bindings with enough letters to need underscores +yet). Here's a program containing an example function definition: + +```rust +fn main() { + println!("Hello, world!"); + + another_function(); +} + +fn another_function() { + println!("Another function."); +} +``` + +Function definitions in Rust start with `fn` and have a set of parentheses +after the function name. The curly braces tell the compiler where the function +body begins and ends. + +We can call any function we’ve defined by entering its name followed by a pair +of parentheses. Since `another_function()` is defined in the program, it can be +called from inside the `main()` function. Note that we defined +`another_function()` _after_ the `main()` function in our source code; we could +have defined it before as well. Rust doesn’t care where you define your +functions, only that they are defined somewhere. + +Let’s start a new project to explore functions further. Open a terminal, and +navigate to the directory you're keeping your projects in. From there, use +Cargo to generate a new project, as follows: + +```bash +$ cargo new --bin functions +$ cd functions +``` + +Place the `another_function()` example in a file named *src/main.rs* and run +it. You should see the following output: + +```bash +$ cargo run + Compiling functions v0.1.0 (file:///projects/functions) + Running `target/debug/functions` +Hello, world! +Another function. +``` + +The lines execute in the order they appear in the `main()` function. First, our +“Hello, world!” message prints, and then `another_function()` is called and its +message is printed. + +### Function Arguments + +Functions can also take arguments. The following rewritten version of +`another_function()` shows what arguments look like in Rust: + +```rust +fn main() { + another_function(5); +} + +fn another_function(x: i32) { + println!("The value of x is: {}", x); +} +``` + +Try running this program, and you should get this output: + +```bash +$ cargo run + Compiling functions v0.1.0 (file:///projects/functions) + Running `target/debug/functions` +The value of x is: 5 +``` + +Since we passed `5` to `another_function()`, the `println!` macro put `5` where +the pair of curly braces were in the format string. The declaration of +`another_function()` shows that it takes one argument named `x`, and the type +of `x` is `i32`. + +In function signatures, we _must_ declare the type. This is a deliberate +decision in the design of Rust; requiring type annotations in function +definitions means the compiler almost never needs you to use them elsewhere in +the code in order to figure out what you mean. + +When you want a function to have multiple arguments, just separate them inside +the function signature with commas, like this: + +```rust +fn main() { + another_function(5, 6); +} + +fn another_function(x: i32, y: i32) { + println!("The value of x is: {}", x); + println!("The value of y is: {}", y); +} +``` + +In this example, we make a function with two arguments, both of which are +`i32`s. If your function has multiple arguments, they don’t need to be the same +type, but they just happen to be in this example. Our function then prints out +the values of both of its arguments. + +Let’s try out this code. Replace the program currently in your `function` +project's `main.rs` file with the example above, and run it as follows: + +```bash +$ cargo run + Compiling functions v0.1.0 (file:///projects/functions) + Running `target/debug/functions` +The value of x is: 5 +The value of y is: 6 +``` + +Since `5` is passed as the `x` argument and `6` is passed as the `y` argument, +the two strings are printed with these values. + +### Function Bodies + +Function bodies are made up of a series of statements ending in an optional +expression. So far, we've only seen functions without an ending expression, but +we have seen expressions as parts of statements. Since Rust is an +expression-based language, this is an important distinction to understand. +Other languages don't have the same distinctions, so let's look at what +statements and expressions are and how their differences affect the bodies of +functions. + +#### Statements and Expressions + +We've already been using both statements and expressions. *Statements* are +instructions that perform some action and do not return a value. *Expressions* +evaluate to a resulting value. Let's look at some examples. + +`Let` bindings are statements. They instruct the program to create a binding +name and assign a value to it. `let y = 6;` in this example is a statement: + +```rust +fn main() { + let y = 6; +} +``` + +Function definitions are also statements-- so the entire previous example is a +statement as well. + +Statements do not return values themselves. Therefore, you can’t assign a `let` +binding to another binding, as this code tries to do: + +```rust,ignore +fn main() { + let x = (let y = 6); +} +``` + +If we were to run this program, we’d get an error like this: + +```bash +$ cargo run + Compiling functions v0.1.0 (file:///projects/functions) +error: expected expression, found statement (`let`) + --> src/main.rs:2:14 +2 |> let x = (let y = 6); + |> ^^^ +note: variable declaration using `let` is a statement + +error: aborting due to previous error +error: Could not compile `functions`. +``` + +The `let y = 6` statement does not return a value, so there isn't anything for +`x` to bind to. This is different than in other languages like C and Ruby where +the assignment returns the value of the assignment. In those languages, you +could write `x = y = 6` and have both `x` and `y` have the value `6`, but that +is not the case in Rust. + +Expressions are most of the rest of the code that you will write in Rust. +Consider a simple math operation, like this: + +```rust,ignore +5 + 6 +``` + +This is an expression, and evaluating it results in the value `11`. Expressions +can be part of statements-- in the previous example that had the statement `let +y = 6;`, `6` is an expression that evaluates to the value `6`. Calling a +function is an expression. Calling a macro is an expression. The block that we +use to create new scopes, `{}`, is an expression, for example: + +```rust +fn main() { + let x = 5; + + let y = { + let x = 3; + x + 1 + }; + + println!("The value of y is: {}", y); +} +``` + +The expression: + +```rust,ignore +{ + let x = 3; + x + 1 +} +``` + +is a block that, in this case, gets evaluated to `4`, which then gets bound to +`y` as part of the `let` statement. + +Note that the line containing `x + 1` does not have a semicolon at the end like +most of the lines we've seen up until now have had. This is the most important +distinction between expressions and statements to remember: statements end in +semicolons while expressions do not. If you add a semicolon to the end of an +expression, that will turn it into a statement, which will then not return a +value. Keep this in mind as we explore function return values and expressions. + +### Functions with Return Values + +Functions can return values back to the code that calls them. We don’t name +return values, but we do declare their type, after an arrow (`->`). In Rust, +the "return value of the function” is synonymous with “the value of the final +expression in the block of the body of a function.” Here's an example of a +function that returns a value: + +```rust +fn five() -> i32 { + 5 +} + +fn main() { + let x = five(); + + println!("The value of x is: {}", x); +} +``` + +There are no function calls, macros, or even `let` statements in the `five()` +function-- just the number `5` by itself. That's a perfectly valid function in +Rust. Note the function's return type, too. Try running this code, and the +output should look like this: + +```bash +$ cargo run + Compiling functions v0.1.0 (file:///projects/functions) + Running `target/debug/functions` +The value of x is: 5 +``` + +The `5` in `five()` is actually the function's return value, which is why the +return type is `i32`. Let’s examine this in more detail. There are two +important bits. First, the line `let x = five();` in `main()` shows that we can +use the return value of a function to initialize a binding. + +Because the function `five()` returns a `5`, that line is the same as saying: + +```rust +let x = 5; +``` + +The second interesting bit is the `five()` function itself. It requires no +arguments and defines the type of the return value, but the body of the +function is a lonely `5` with no semicolon because it is an expression whose +value we want to return. Let's look at another example: + +```rust +fn main() { + let x = plus_one(5); + + println!("The value of x is: {}", x); +} + +fn plus_one(x: i32) -> i32 { + x + 1 +} +``` + +Running this code will print `The value of x is: 6`. What happens if we put a +semicolon at the end of the line containing `x + 1`, changing it from an +expression to a statement? + +```rust,ignore +fn main() { + let x = plus_one(5); + + println!("The value of x is: {}", x); +} + +fn plus_one(x: i32) -> i32 { + x + 1; +} +``` + +Running this code gives an error, as follows: + +```bash +$ cargo run + Compiling functions v0.1.0 (file:///projects/functions) +error: not all control paths return a value [--explain E0269] + --> src/main.rs:7:1 +7 |> fn plus_one(x: i32) -> i32 { + |> ^ +help: consider removing this semicolon: + --> src/main.rs:8:10 +8 |> x + 1; + |> ^ + +error: aborting due to previous error +error: Could not compile `functions`. +``` + +The main error message, "not all control paths return a value", reveals the +core of the issue with this code. The definition of the function `plus_one` +says that it will return an `i32`, but statements don’t evaluate to a value. +Therefore, nothing is returned, which contradicts the function definition and +results in an error. In this output, Rust gives an option to rectify this: it +suggests removing the semicolon, which would fix the error. + +## Comments + +All programmers strive to make their code easy to understand, but sometimes +extra explanation is warranted. In these cases, we leave notes in our source +code that the compiler will ignore but people reading the source code may find +useful. These notes are called *comments*. + +Here’s a simple comment: + +```rust +// Hello, world. +``` + +In Rust, comments must start with two slashes and will last until the end of +the line. For comments that extend beyond a single line, you'll need to +include `//` on each line, like this: + +```rust +// So we’re doing something complicated here, long enough that we need +// multiple lines of comments to do it! Whew! Hopefully, this comment will +// explain what’s going on. +``` + +Comments can also be placed at the end of lines of code: + +```rust +fn main() { + let lucky_number = 7; // I’m feeling lucky today. +} +``` + +But you’ll more often see them above, like so: + +```rust +fn main() { + // I’m feeling lucky today. + let lucky_number = 7; +} +``` + +That’s all there is to it. Comments are not particularly complicated. + +## Control Flow + +Deciding whether or not to run some code depending on if a condition is true, +or deciding to run some code repeatedly while a condition is true, are basic +building blocks in most programming languages. The most common constructs that +let us control the flow of execution of our Rust code are `if` expressions and +loops. + +### `if` Expressions + +An `if` expression allows us to branch our code depending on conditions. We +provide a condition and then say, "If this condition is met, then run this +block of code. If the condition is not met, do not run this block of code." + +Let’s make a new project to explore `if`. Navigate to your projects directory, +and use Cargo to make a new project called `branches`: + +```bash +$ cargo new --bin branches +$ cd branches +``` + +Write this sample program using `if` and save it in the *branches* directory in +`src/main.rs`: + +```rust +fn main() { + let number = 3; + + if number < 5 { + println!("condition was true"); + } else { + println!("condition was false"); + } +} +``` + +All `if` expressions start with `if`, which is followed by a condition. In this +case, our condition is checking if our variable binding `number` has a value +that is less than 5. The block of code we want to execute if the condition is +true goes immediately after the condition, inside curly braces. These blocks +are sometimes called ‘arms’. We can optionally also include an `else` +statement, which we have chosen to do here. `else` gives the program a block of +code to execute should `condition` evaluate to false. + +Try running this code, and you should see output like this: + +```bash +$ cargo run + Compiling branches v0.1.0 (file:///projects/branches) + Running `target/debug/branches` +condition was true +``` + +Let’s try changing the value of `number` to a value that makes the condition +`false` to see what happens: + +```rust,ignore +let number = 7; +``` + +Run the program again, and look at the output: + +```bash +$ cargo run + Compiling branches v0.1.0 (file:///projects/branches) + Running `target/debug/branches` +condition was false +``` + +It’s also worth noting that `condition` here _must_ be a `bool`. To see what +happens if the condition isn't a `bool`, try running this code: + +```rust,ignore +fn main() { + let number = 3; + + if number { + println!("number was three"); + } +} +``` + +The `if` condition evaluates to a value of `3` this time, and Rust will +complain about it: + +```bash + Compiling branches v0.1.0 (file:///projects/branches) +error: mismatched types [--explain E0308] + --> src/main.rs:4:8 +4 |> if number { + |> ^^^^^^ expected bool, found integral variable +note: expected type `bool` +note: found type `_` + +error: aborting due to previous error +Could not compile `branches`. +``` + +The error tells us that Rust expected a `bool`, but got an integer. Rust will +not automatically try to convert non-boolean types to a boolean here, unlike +languages like Ruby or JavaScript. We must be explicit and always give `if` a +`boolean` as its condition. If your intention is for the `if` code block to be run if a number is not equal to `0`, for example, we would change the `if` expression to read: + +```rust +fn main() { + let number = 3; + + if number != 0 { + println!("number was something other than zero"); + } +} +``` + +Running this will print "number was something other than zero". + +#### Multiple Conditions with `else if` + +We can have multiple coniditions by combining `if` and `else` in an `else if` +expression. For example: + +```rust +fn main() { + let number = 5; + + if number == 3 { + println!("condition was 3"); + } else if number == 4 { + println!("condition was 4"); + } else if number == 5 { + println!("condition was 5"); + } else { + println!("condition was something else"); + } +} +``` + +This program has four possible paths it can take. If you try running it, you +should see output like this: + +```bash +$ cargo run + Compiling branches v0.1.0 (file:///projects/branches) + Running `target/debug/branches` +condition was 5 +``` + +When this program executes, it will check each `if` expression in turn and +execute the first body for which the condition holds true. + +Using too many `else if` expressions can clutter your code, so if you find +yourself with more than one, you may want to look at refactoring your code. In +Chapter XX, we'll talk about a powerful Rust branching construct called `match` +for these cases. + +#### Using `if` in a Binding + +The last detail you need to learn about `if` is that it’s an expression. That +means that we can use it on the right hand side of a `let` binding, for +instance: + +```rust +fn main() { + let condition = true; + let number = if condition { + 5 + } else { + 6 + }; + + println!("The value of number is: {}", number); +} +``` + +The `number` variable will be bound to a value based on the outcome of the `if` +expression. Let’s run this to see what happens: + +```bash +$ cargo run + Compiling branches v0.1.0 (file:///projects/branches) + Running `target/debug/branches` +The value of number is: 5 +``` + +Remember, blocks of code evaluate to the last expression in them, and numbers +by themselves are also expressions. In this case, the value of the whole `if` +expression depends on which block of code executes. This means that the value +that results from both arms of the `if` must be the same type; in the previous +example, they were both `i32` integers. But what happens if the types are +mismatched, as in the following example? + +```rust,ignore +fn main() { + let condition = true; + + let number = if condition { + 5 + } else { + "six" + }; + + println!("The value of number is: {}", number); +} +``` + +The expression in the `if` block is an integer and the expresion in the `else` +block is a string. This can’t work, because variable bindings must have a +single type. If we try to run this, we’ll get an error: + +```bash + Compiling branches v0.1.0 (file:///projects/branches) +src/main.rs:4:18: 8:6 error: if and else have incompatible types: + expected `_`, + found `&‘static str` +(expected integral variable, + found &-ptr) [E0308] +src/main.rs:4 let number = if condition { +src/main.rs:5 5 +src/main.rs:6 } else { +src/main.rs:7 "six" +src/main.rs:8 }; +src/main.rs:4:18: 8:6 help: run `rustc --explain E0308` to see a detailed explanation +error: aborting due to previous error +Could not compile `branches`. +``` + +The `if` and `else` arms have value types that are incompatible, and Rust tells +us exactly where to find the problem in our program. + +### Repetition with Loops + +It’s often useful to be able to execute a block of code more than one time. For +this, Rust has several constructs called *loops*. A loop runs through the code +inside it to the end and then starts immediately back at the beginning. To try +out loops, let’s make a new project. Navigate to your *projects* folder and use +Cargo to make a new project: + +```bash +$ cargo new --bin loops +$ cd loops +``` + +There are three kinds of loops in Rust: `loop`, `while`, and `for`. Let’s dig +in. + +#### Repeating Code with `loop` + +The `loop` keyword tells Rust to execute a block of code over and over again +forever or until we explicitly tell it to stop. + +For an example, change the *src/main.rs* file in your *loops* directory to look +like this: + +```rust,ignore +fn main() { + loop { + println!("again!"); + } +} +``` + +If we run this program, we’ll see `again!` printed over and over continuously +until we stop the program manually. Most terminals support a keyboard shortcut, +`control-c`, to halt a program stuck in a continual loop. Give it a try: + +```bash +$ cargo run + Compiling loops v0.1.0 (file:///projects/loops) + Running `target/debug/loops` +again! +again! +again! +again! +^Cagain! +``` + +That `^C` there is where we hit `control-c`. You may or may not see "again!" +printed after the `^C`, depending on where the code was in the loop when it +received the signal to halt. + +Fortunately, Rust provides another, more reliable way to break out of a loop. +We can place the `break` keyword within the loop to tell the program when to +stop executing the loop. Recall that we did this in the guessing game to exit +the program when the user won the game by guessing the number correctly. + +#### Conditional Loops With `while` + +A useful thing that many programs do is have a condition that can be evaluated +within a loop. While the condition is true, the loop runs. When the condition +ceases to be true, we call `break`, stopping the loop. This could be +implemented with a combination of `loop`, `if`, `else`, and `break`; try to do +that now if you'd like! + +But this pattern is so common that Rust has a more efficient language construct +for it, called a `while` loop. Here's an example using `while`: this program +loops three times, counting down each time. Finally, after the loop, it prints +another message, then exits: + +```rust +fn main() { + let mut number = 3; + + while number != 0 { + println!("{}!", number); + + number = number - 1; + } + + println!("LIFTOFF!!!"); +} +``` + +This gets rid of a lot of nesting that would be necessary if we used `loop`, +`if`, `else`, and `break`, and it's more clear. While a condition holds, run +this code; otherwise, exit the loop. + +#### Looping Though a Collection with `for` + +We could use this `while` construct to loop over the elements of a collection, +like an array. For example: + +```rust +fn main() { + let a = [10, 20, 30, 40, 50]; + let mut index = 0; + + while index < 5 { + println!("the value is: {}", a[index]); + + index = index + 1; + } +} +``` + +Here, we're counting up through the elements in the array. We start at index 0, +then loop until we hit the final index of our array (that is, when `index < 5` +is no longer true). Running this will print out every element of the array: + +```bash +$ cargo run + Compiling loops v0.1.0 (file:///projects/loops) + Running `target/debug/loops` +the value is: 10 +the value is: 20 +the value is: 30 +the value is: 40 +the value is: 50 +``` + +All five array values appear in the terminal, as expected. Even though `index` +will reach a value of `6` at some point, the loop stops executing before trying +to fetch a sixth value from the array. + +This approach is error-prone, though; we could cause our program to panic by +getting the index length incorrect. It's also slow, as the compiler needs to +perform the conditional check on every element on every iteration through the +loop. + +As a more efficient alternative, we can use a `for` loop and execute some code +for each item in a collection. A `for` loop looks like this: + +```rust +fn main() { + let a = [10, 20, 30, 40, 50]; + + for element in a.iter() { + println!("the value is: {}", element); + } +} +``` + +If we run this, we'll see the same output as the previous example. Importantly, +though, we've now increased the safety of our code and eliminated the chance of +bugs resulting from going beyond the end of the array or not going far enough +and missing some items. + +For example, in the previous code that uses the `while` loop, if we removed an +item from the `a` array but forgot to update the condition to be `while index < +4`, our code would panic. Using the `for` loop means we would not need to +remember to change any other code if we changed the the number of values in the +array. + +If you're wondering about the `.iter()` code in this example, keep reading! We +will cover method syntax generally in Chapter XX and iterators specifically in +Chapter XX. + +The safety and conciseness of `for` loops make them the most commonly used loop +construct in Rust. Even in situations where you want to run some code a certain +number of times, like our countdown example that used a `while` loop, most +Rustaceans would use a `for` loop. The way to do that is using a `Range`, which +is a type provided by the standard library that generates numbers starting from +one number and ending before another number. Here's what the countdown would +look like with a for loop, and using another method we haven't yet talked +about, `.rev()`, to reverse the range: + +```rust +fn main() { + for number in (1..4).rev() { + println!("{}!", number); + } + println!("LIFTOFF!!!"); +} +``` + +That's a bit nicer, isn't it? + +Now that you know how Rust does things that most other languages can do, let's +talk about a concept that _doesn't_ commonly exist: ownership. diff --git a/nostarch/chapter4.md b/nostarch/chapter04.md similarity index 54% rename from nostarch/chapter4.md rename to nostarch/chapter04.md index 6e5dcf0e7..16753d8ae 100644 --- a/nostarch/chapter4.md +++ b/nostarch/chapter04.md @@ -1,49 +1,52 @@ -# Ownership +# Understanding Ownership + +Ownership is important to understand: it's Rust's most unique feature, and +enables Rust to make memory safety guarantees without needing a garbage +collector. We’ll also talk about several related features: borrowing, slices, +and how Rust lays things out in memory. + +## Ownership Rust’s central feature is called ‘ownership’. It is a feature that is straightforward to explain, but has deep implications for the rest of the -language. +language. -Rust is committed to both safety and speed. One of the key tools for balancing -between them is “zero-cost abstractions”: the various abstractions in Rust do -not pose a global performance penalty. The ownership system is a prime example -of a zero-cost abstraction. All of the analysis we’ll talk about in this guide -is done at compile time. You do not pay any run-time cost for any of these +All programs have to manage the way they use a computer's memory while running. +Some languages have garbage collection, while in others, the programmer has to +explicitly allocate and free the memory. Rust takes a third approach: memory is +managed through a system of ownership with a set of rules that the compiler +checks at compile-time. You do not pay any run-time cost for any of these features. -However, this system does have a certain cost: learning curve. Many new -Rustaceans experience something we like to call ‘fighting with the borrow -checker’, where the Rust compiler refuses to compile a program that the author -thinks is valid. This can happen because the programmer isn’t used to thinking -carefully about ownership, or is thinking about it differently from the way -that Rust does. You probably will experience something similar at first. There is -good news, however: more experienced Rust developers report that once they work -with the rules of the ownership system for a period of time, they fight the -borrow checker less and less. Keep at it! +However, because ownership is a new concept for many programmers, it does take +some time to get used to. There is good news, though: the more experienced you +become with Rust and the rules of the ownership system, the more you'll be +able to naturally develop code that is both safe and efficient. Keep at it! -This chapter will give you a foundation for understanding the rest of the -language. To do so, we’re going to learn through examples, focusing on a very -common data structure: strings. +Once you understand ownership, you have a good foundation for understanding the +features that make Rust unique. In this chapter, we'll learn ownership by going +through some examples, focusing on a very common data structure: strings. -## Variable binding scope +### Variable binding scope -Let’s take a step back and look at the very basics again. Now that we’re past -basic syntax, we won’t include all of the `fn main() {` stuff in examples, so -if you’re following along, you will have to put them inside of a `main()` -function. This lets our examples be a bit more concise, letting us focus on the -actual details, rather than boilerplate. +We've walked through an example of a Rust program already in the tutorial +chapter. Now that we’re past basic syntax, we won’t include all of the `fn +main() {` stuff in examples, so if you’re following along, you will have to put +them inside of a `main()` function. This lets our examples be a bit more +concise, letting us focus on the actual details rather than boilerplate. -Anyway, here it is: +Anyway, here is our first example: ```rust let s = "hello"; ``` -This variable binding refers to a string literal. It’s valid from the point at -which it’s declared, until the end of the current _scope_. That is: +This variable binding refers to a string literal, where the value of the string +is hard coded into the text of our program. The binding is valid from the point +at which it’s declared until the end of the current _scope_. That is: ```rust -{ // s is not valid here, it’s not yet in scope +{ // s is not valid here, it’s not yet declared let s = "hello"; // s is valid from this point forward // do stuff with s @@ -55,36 +58,38 @@ In other words, there are two important points in time here: - When `s` comes ‘into scope’, it is valid. - It remains so until it ‘goes out of scope’. -At this point, things are similar to other programming languages. Let’s build -on top of this understanding by introducing a new type: `String`. +At this point, things are similar to other programming languages. Now let’s +build on top of this understanding by introducing the `String` type. -## Strings +### Strings -String literals are convenient, but they aren’t the only way that you use strings. -For one thing, they’re immutable. For another, not every string is literal: -what about taking user input and storing it in a string? +String literals are convenient, but they aren’t the only way that you use +strings. For one thing, they’re immutable. For another, not every string is +literal: what about taking user input and storing it in a string? -For this, Rust has a second string type, `String`. You can create a `String` from -a string literal using the `from` function: +For this, Rust has a second string type, `String`. You can create a `String` +from a string literal using the `from` function: ```rust let s = String::from("hello"); ``` -We haven’t seen the double colon (`::`) syntax yet. It is a kind of scope -operator, allowing us to namespace this particular `from()` function under the -`String` type itself, rather than using some sort of name like `string_from()`. -We’ll discuss this syntax more in the “Method Syntax” and “Modules” chapters. +We haven’t seen the double colon (`::`) syntax yet. It is an operator that +allows us to namespace this particular `from()` function under the `String` +type itself, rather than using some sort of name like `string_from()`. We’ll +discuss this syntax more in the “Method Syntax” and “Modules” chapters. This kind of string can be mutated: ```rust let mut s = String::from("hello"); -s.push_str(", world!"); +s.push_str(", world!"); // push_str() appends a literal to a String + +println!("{}", s); // This will print `hello, world!` ``` -## Memory and allocation +### Memory and allocation So, what’s the difference here? Why can `String` be mutated, but literals cannot? The difference comes down to how these two types deal with memory. @@ -92,11 +97,13 @@ cannot? The difference comes down to how these two types deal with memory. In the case of a string literal, because we know the contents of the string at compile time, we can hard-code the text of the string directly into the final executable. This means that string literals are quite fast and efficient. But -these properties only come from its immutability; we can’t put an -arbitrary-sized blob of memory into the binary for each string! +these properties only come from its immutability. Unfortunately, we can’t put a +blob of memory into the binary for each string whose size is unknown at compile +time and whose size might change over the course of running the program. -With `String`, to support a mutable, growable string, we need to allocate an -unknown amount of memory to hold the contents. This means two things: +With `String`, in order to support a mutable, growable string, we need to +allocate an unknown amount of memory to hold the contents. This means two +things: 1. The memory must be requested from the operating system at runtime. 2. We need a way of giving this memory back to the operating system when we’re @@ -107,13 +114,15 @@ implementation requests the memory it needs. This is pretty much universal in programming languages. The second case, however, is different. In languages with a garbage collector -(‘GC’), the GC handles that second case, and we, as the programmer, don’t need -to think about it. Without GC, it’s the programmer’s responsibility to identify -when memory is no longer being used, and explicitly return it, just as it was -requested. Doing this correctly has historically been a difficult problem. If -we forget, we will waste memory. If we do it too early, we will have an invalid -variable. If we do it twice, that’s a bug too. We need to pair exactly one -`allocate()` with exactly one `free()`. +(‘GC’), the GC will keep track and clean up memory that isn't being used +anymore, and we, as the programmer, don’t need to think about it. Without GC, +it’s our responsibility to identify when memory is no longer being used and +call code to explicitly return it, just as we did to request it. Doing this +correctly has historically been a difficult problem. If we forget, we will +waste memory. If we do it too early, we will have an invalid variable. If we do +it twice, that’s a bug too. We need to pair exactly one `allocate()` with +exactly one `free()`. + Rust takes a different path. Remember our example? Here’s a version with `String`: @@ -126,22 +135,23 @@ Rust takes a different path. Remember our example? Here’s a version with } // this scope is now over, and s is no longer valid ``` -We have a natural point at which we can return the memory our `String` needs back -to the operating system: when it goes out of scope! When a variable goes out of -scope, a special function is called. This function is called `drop()`, and it -is where the author of `String` can put the code to return the memory. +We have a natural point at which we can return the memory our `String` needs +back to the operating system: when it goes out of scope. When a variable goes +out of scope, Rust calls a special function for us. This function is called +`drop()`, and it is where the author of `String` can put the code to return the +memory. -> Aside: This pattern is sometimes called “Resource Aquisition Is +> Aside: This pattern is sometimes called “Resource Acquisition Is > Initialization” in C++, or “RAII” for short. While they are very similar, > Rust’s take on this concept has a number of differences, and so we don’t tend -> to use the same term. If you’re familliar with this idea, keep in mind that it +> to use the same term. If you’re familiar with this idea, keep in mind that it > is _roughly_ similar in Rust, but not identical. This pattern has a profound impact on the way that Rust code is written. It may -seem obvious right now, but things can get tricky in more advanced situations! +seem obvious right now, but things can get tricky in more advanced situations. Let’s go over the first one of those right now. -## Move +### Move What would you expect this code to do? @@ -150,7 +160,7 @@ let x = 5; let y = x; ``` -You might say “Make a copy of `5`.” That’d be correct! We now have two +You might say “Make a copy of `5`”, and that would be correct. We now have two bindings, `x` and `y`, and both equal `5`. Now let’s look at `String`. What would you expect this code to do? @@ -164,57 +174,52 @@ You might say “copy the `String`!” This is both correct and incorrect at the same time. It does a _shallow_ copy of the `String`. What’s that mean? Well, let’s take a look at what `String` looks like under the covers: -string +string A `String` is made up of three parts: a pointer to the memory that holds the contents of the string, a length, and a capacity. The length is how much memory the `String` is currently using. The capacity is the total amount of memory the `String` has gotten from the operating system. The difference between length -and capacity matters, but not in this context, so don’t worry about it too much -if it doesn’t make sense, and just ignore the capacity. +and capacity matters but not in this context, so don’t worry about it too much. +For right now, it's fine to ignore the capacity. -> We’ve talked about two kinds of composite types: arrays and tuples. `String` -> is a third type: a `struct`, which we will cover the details of in the next -> chapter of the book. For now, thinking about `String` as a tuple is close -> enough. +When we assign `s1` to `s2`, the `String` itself is copied, meaning we copy the +pointer, the length, and the capacity. We do not copy the data that the +`String`'s pointer refers to. In other words, it looks like this: -When we assign `s1` to `s2`, the `String` itself is copied. But not all kinds -of copying are the same. Many people draw distinctions between ‘shallow -copying’ and ‘deep copying’. We don’t use these terms in Rust. We instead say -that something is ‘moved’ or ‘cloned’. Assignment in Rust causes a ‘move’. In -other words, it looks like this: - -s1 and s2 +s1 and s2 _Not_ this: -s1 and s2 to two places +s1 and s2 to two places -When moving, Rust makes a copy of the data structure itself, the contents of -`s1` are copied, but if `s1` contains a reference, like it does in this case, -Rust will not copy the things that those references refer to. +There’s a problem here. Both data pointers are pointing to the same place. Why +is this a problem? Well, when `s2` goes out of scope, it will free the memory +that the pointer points to. And then `s1` goes out of scope, and it will _also_ +try to free the memory that the pointer points to. That’s bad, and is known as +a "double free" error. -There’s a problem here! Both `data` pointers are pointing to the same place. -Why is this a problem? Well, when `s2` goes out of scope, it will free the -memory that `data` points to. And then `s1` goes out of scope, and it will -_also_ try to free the memory that `data` points to! That’s bad. +So what’s the solution? Here, we stand at a crossroads with a few options. -So what’s the solution? Here, we stand at a crossroads. There are a few -options. One would be to declare that assignment will also copy out any data. -This works, but is inefficient: what if our `String` contained a novel? Also, -it only works for memory. What if, instead of a `String`, we had a -`TcpConnection`? Opening and closing a network connection is very similar to -allocating and freeing memory. The solution that we could use there is to allow -the programmer to hook into the assignment, similar to `drop()`, and write code -fix things up. That would work, but now, an `=` can run arbitrary code. That’s -also not good, and it doesn’t solve our efficiency concerns either. +One way would be to change assignment so that it will also copy out any data. +This works, but is inefficient: what if our `String` contained a novel? +Also, that solution would only work for memory. What if, instead of a `String`, +we had a `TcpConnection`? Opening and closing a network connection is very +similar to allocating and freeing memory, so it would be nice to be able to use +the same mechanism. We wouldn't be able to, though, because creating a new +connection requires more than just copying memory: we have to request a new +connection from the operating system. We could then extend our solution to +allow the programmer to hook into the assignment, similar to `drop()`, and +write code to fix things up. That would work, but if we did that, an `=` could +run arbitrary code. That’s also not good, and it doesn’t solve our efficiency +concerns either. Let’s take a step back: the root of the problem is that `s1` and `s2` both -think that they have control of the memory, and therefore needs to free it. +think that they have control of the memory and therefore need to free it. Instead of trying to copy the allocated memory, we could say that `s1` is no -longer valid, and therefore, doesn’t need to free anything. This is in fact the -choice that Rust makes. Check it out what happens when you try to use `s1` -after `s2` is created: +longer valid and, therefore, doesn’t need to free anything. This is in fact the +choice that Rust makes. Check out what happens when you try to use `s1` after +`s2` is created: ```rust,ignore let s1 = String::from("hello"); @@ -225,7 +230,7 @@ println!("{}", s1); You’ll get an error like this: -```text +```bash 5:22 error: use of moved value: `s1` [E0382] println!("{}", s1); ^~ @@ -235,30 +240,38 @@ println!("{}", s1); ^~ ``` -We say that `s1` was _moved_ into `s2`. When a value moves, its data is copied, -but the original variable binding is no longer usable. That solves our problem: +If you have heard the terms "shallow copy" and "deep copy" while working with +other languages, the concept of copying the pointer, length, and capacity +without copying the data probably sounded like a shallow copy. Because Rust +also invalidates the first binding, instead of calling this a shallow copy, +it's called a _move_. Here we would read this by saying that `s1` was _moved_ +into `s2`. So what actually happens looks like this: -s1 and s2 to the same place +s1 and s2 to the same place -With only `s2` valid, when it goes out of scope, it will free the memory, and we’re done! +That solves our problem! With only `s2` valid, when it goes out of scope, it +alone will free the memory, and we’re done. -## Ownership Rules +### Ownership Rules This leads us to the Ownership Rules: -> 1. Each value in Rust has a variable binding that’s called it’s ‘owner’. +> 1. Each value in Rust has a variable binding that’s called its ‘owner’. > 2. There can only be one owner at a time. > 3. When the owner goes out of scope, the value will be `drop()`ped. Furthermore, there’s a design choice that’s implied by this: Rust will never -automatically create ‘deep’ copies of your data. Any automatic copying must be -inexpensive. +automatically create ‘deep’ copies of your data. Therefore, any _automatic_ +copying can be assumed to be inexpensive. -## Clone +### Clone -But what if we _do_ want to deeply copy the `String`’s data, and not just the -`String` itself? There’s a common method for that: `clone()`. Here’s an example -of `clone()` in action: +But what if we _do_ want to deeply copy the `String`’s data and not just the +`String` itself? There’s a common method for that: `clone()`. We will discuss +methods in the section on [`structs`], but they’re a common enough feature +in many programming languages that you have probably seen them before. + +Here’s an example of the `clone()` method in action: ```rust let s1 = String::from("hello"); @@ -267,16 +280,18 @@ let s2 = s1.clone(); println!("{}", s1); ``` +[`structs`]: ch05-01-structs.html + This will work just fine. Remember our diagram from before? In this case, it _is_ doing this: -s1 and s2 to two places +s1 and s2 to two places When you see a call to `clone()`, you know that some arbitrary code is being -executed, which may be expensive. It’s a visual indicator that something +executed, and that code may be expensive. It’s a visual indicator that something different is going on here. -## Copy +### Copy There’s one last wrinkle that we haven’t talked about yet. This code works: @@ -289,38 +304,32 @@ println!("{}", x); But why? We don’t have a call to `clone()`. Why didn’t `x` get moved into `y`? -For types that do not have any kind of complex storage requirements, like -integers, typing `clone()` is busy work. There’s no reason we would ever want -to prevent `x` from being valid here, as there’s no situation in which it’s -incorrect. In other words, there’s no difference between deep and shallow -copying here, so calling `clone()` wouldn’t do anything differently from the -usual shallow copying. +Types like integers that have a known size at compile time do not ask for +memory from the operating system and therefore do not need to be `drop()`ped +when they go out of scope. That means there's no reason we would want to +prevent `x` from being valid after we create the binding `y`. In other words, +there’s no difference between deep and shallow copying here, so calling +`clone()` wouldn’t do anything differently from the usual shallow copying and +we can leave it out. -Rust has a special annotation that you can place on types, called `Copy`. If -a type is `Copy`, an older binding is still usable after assignment. Integers -are an example of such a type; most of the primitive types are `Copy`. - -While we haven’t talked about how to mark a type as `Copy` yet, you might ask -yourself “what happens if we made `String` `Copy`?” The answer is, you cannot. -Remember `drop()`? Rust will not let you make something `Copy` if it has +Rust has a special annotation that you can place on types like these, and that +annotation is the `Copy` trait. We'll talk more about traits in Chapter XX. If +a type has the `Copy` trait, an older binding is still usable after assignment. +Rust will not let you make something have the `Copy` trait if it has implemented `drop()`. If you need to do something special when the value goes out of scope, being `Copy` will be an error. So what types are `Copy`? You can check the documentation for the given type to -be sure, but as a rule of thumb, any group of simple scalar values can be -Copy, but nothing that requires allocation or is some form of resource is `Copy`. -And you can’t get it wrong: the compiler will throw an error if you try to use -a type that moves incorrectly, as we saw above. - -Here’s some types that you’ve seen so far that are `Copy`: +be sure, but as a rule of thumb, any group of simple scalar values can be Copy, +but nothing that requires allocation or is some form of resource is `Copy`. Here’s some of the types that are `Copy`: * All of the integer types, like `u32`. * The booleans, `true` and `false`. * All of the floating point types, like `f64`. * Tuples, but only if they contain types which are also `Copy`. `(i32, i32)` - is `Copy`, but `(i32, String)` is not! + is `Copy`, but `(i32, String)` is not. -## Ownership and functions +### Ownership and functions Passing a value to a function has similar semantics as assigning it: @@ -374,7 +383,9 @@ fn makes_copy(some_integer: i32) { // some_integer comes into scope. ``` Remember: If we tried to use `s` after the call to `takes_ownership()`, Rust -would throw a compile-time error! These static checks protect us from mistakes. +would throw a compile-time error. These static checks protect us from mistakes. +Try adding code to `main` that uses `s` and `x` to see where you can use them +and where the ownership rules prevent you from doing so. Returning values can also transfer ownership: @@ -399,7 +410,7 @@ fn takes_and_gives_back(a_string: String) -> String { } ``` -With simililar annotations: +With similiar annotations: ```rust fn main() { @@ -435,9 +446,11 @@ fn takes_and_gives_back(a_string: String) -> String { // a_string comes into sco It’s the same pattern, every time: assigning something moves it, and when an owner goes out of scope, if it hasn’t been moved, it will `drop()`. -This might seem a bit tedious, and it is. What if I want to let a function use -a value, but not take ownership? It’s quite annoying that anything I pass in -also needs passed back. Look at this function: +This might seem a bit tedious, and it is. What if we want to let a function use +a value but not take ownership? It’s quite annoying that anything we pass in +also needs to be passed back if we want to use it again, in addition to any +data resulting from the body of the function that we might want to return as +well. It's _possible_ to return multiple values, using a tuple, like this: ```rust fn main() { @@ -455,13 +468,11 @@ fn calculate_length(s: String) -> (String, usize) { } ``` -This is too much ceremony: we have to use a tuple to give back the `String` as -well as the length. It’s a lot of work for a pattern that should be common. +But this is too much ceremony and a lot of work for a concept that should be +common. Luckily for us, Rust has a feature for this concept, and it’s what the +next section is about. -Luckily for us, Rust has such a feature, and it’s what the next section is about. - - -# References and Borrowing +## References and Borrowing At the end of the last section, we had some example Rust that wasn’t very good. Here it is again: @@ -483,7 +494,8 @@ fn calculate_length(s: String) -> (String, usize) { ``` The issue here is that we have to return the `String` back to the calling -function so that it could still use it. +function so that we can still use it there, since it was moved when we called +`calculate_length()`. There is a better way. It looks like this: @@ -503,11 +515,12 @@ fn calculate_length(s: &String) -> usize { } ``` -First, you’ll notice all of the tuple stuff is gone. Next, that we pass `&s1` -into `calculate_lengths()`. And in its definition, we take `&String` rather -than `String`. +First, you’ll notice all of the tuple stuff in the binding declaration and the +function return value is gone. Next, note that we pass `&s1` into +`calculate_length()`, and in its definition, we take `&String` rather than +`String`. -These `&s` are called ‘references’, and they allow you to refer to some value +These `&`s are called ‘references’, and they allow you to refer to some value without taking ownership of it. Here’s a diagram: DIAGRAM GOES HERE of a &String pointing at a String, with (ptr, len, capacity) @@ -517,7 +530,7 @@ Let’s take a closer look at the function call here: ```rust # fn calculate_length(s: &String) -> usize { # let length = s.len(); -# +# # length # } let s1 = String::from("hello"); @@ -525,14 +538,12 @@ let s1 = String::from("hello"); let len = calculate_length(&s1); ``` -The `&s1` syntax lets us create a reference from `s1`. This reference _refers_ -to the value of `s1`, but does not own it. Because it does not own it, the +The `&s1` syntax lets us create a reference with `s1`. This reference _refers_ +to the value of `s1` but does not own it. Because it does not own it, the value it points to will not be dropped when the reference goes out of scope. Likewise, the signature of the function uses `&` to indicate that it takes -a reference as an argument: - -Let’s add some explanatory annotations: +a reference as an argument. Let’s add some explanatory annotations: ```rust fn calculate_length(s: &String) -> usize { // s is a reference to a String @@ -549,11 +560,11 @@ This lets us write functions which take references as arguments instead of the values themselves, so that we won’t need to return them to give back ownership. There’s another word for what references do, and that’s ‘borrowing’. Just like -with real life, if I own something, you can borrow it from me. When you’re done, -you have to give it back. +with real life, if a person owns something, you can borrow it from them. When +you’re done, you have to give it back. Speaking of which, what if you try to modify something you borrow from me? Try -this code out. Spoiler alert: it doesn’t work: +this code out. Spoiler alert: it doesn’t work! ```rust,ignore fn main() { @@ -563,22 +574,24 @@ fn main() { } fn change(some_string: &String) { - some_string.push_str(", world"); // push_str() appends a literal to a String + some_string.push_str(", world"); } ``` Here’s the error: ```text -8:16 error: cannot borrow immutable borrowed content `*some_string` as mutable - some_string.push_str(", world"); // push_str() appends a literal to a String - ^~~~~~~~~~~ +error: cannot borrow immutable borrowed content `*some_string` as mutable + --> error.rs:8:5 + | +8 | some_string.push_str(", world"); + | ^^^^^^^^^^^ ``` -Just like bindings are immutable by default, so are references. We’re not allowed -to modify something we have a reference to. +Just like bindings are immutable by default, so are references. We’re not +allowed to modify something we have a reference to. -## Mutable references +### Mutable references We can fix this bug! Just a small tweak: @@ -590,12 +603,13 @@ fn main() { } fn change(some_string: &mut String) { - some_string.push_str(", world"); // push_str() appends a literal to a String + some_string.push_str(", world"); } ``` -First, we had to change `s` to be `mut`. Then, we had to create a mutable reference -with `&mut s` and accept a mutable reference with `some_string: &mut String`. +First, we had to change `s` to be `mut`. Then we had to create a mutable +reference with `&mut s` and accept a mutable reference with `some_string: &mut +String`. Mutable references have one big restriction, though. This code fails: @@ -609,28 +623,24 @@ let r2 = &mut s; Here’s the error: ```text -5:20 error: cannot borrow `s` as mutable more than once at a time [E0499] - let r2 = &mut s; - ^ -4:20 note: previous borrow of `s` occurs here; the mutable borrow prevents - subsequent moves, borrows, or modification of `s` until the borrow - ends - let r1 = &mut s; - ^ -7:2 note: previous borrow ends here -fn main() { - -} -^ +error[E0499]: cannot borrow `s` as mutable more than once at a time + --> borrow_twice.rs:5:19 + | +4 | let r1 = &mut s; + | - first mutable borrow occurs here +5 | let r2 = &mut s; + | ^ second mutable borrow occurs here +6 | } + | - first borrow ends here ``` -The error is what it says on the tin: you cannot borrow something more than -once at a time in a mutable fashion. This restriction allows for mutation, but -in a very controlled fashion. It is something that new Rustaceans struggle -with, because most languages let you mutate whenever you’d like. +The error is what it says: you cannot borrow something mutably more than once +at a time. This restriction allows for mutation but in a very controlled +fashion. It is something that new Rustaceans struggle with, because most +languages let you mutate whenever you’d like. As always, we can use `{}`s to create a new scope, allowing for multiple mutable -references. Just not _simultaneous_ ones: +references, just not _simultaneous_ ones: ```rust let mut s = String::from("hello"); @@ -643,7 +653,8 @@ let mut s = String::from("hello"); let r2 = &mut s; ``` -There is a simlar rule for combining the two kinds of references. This code errors: +There is a similar rule for combining mutable and immutable references. This +code errors: ```rust,ignore let mut s = String::from("hello"); @@ -656,27 +667,23 @@ let r3 = &mut s; // BIG PROBLEM Here’s the error: ```text -19: 6:20 error: cannot borrow `s` as mutable because it is also borrowed as - immutable [E0502] - let r3 = &mut s; // BIG PROBLEM - ^ -15: 4:16 note: previous borrow of `s` occurs here; the immutable borrow - prevents subsequent moves or mutable borrows of `s` until the - borrow ends - let r1 = &s; // no problem - ^ -8:2 note: previous borrow ends here -fn main() { - -} -^ +error[E0502]: cannot borrow `s` as mutable because it is also borrowed as immutable + --> borrow_thrice.rs:6:19 + | +4 | let r1 = &s; // no problem + | - immutable borrow occurs here +5 | let r2 = &s; // no problem +6 | let r3 = &mut s; // BIG PROBLEM + | ^ mutable borrow occurs here +7 | } + | - immutable borrow ends here ``` Whew! We _also_ cannot have a mutable reference while we have an immutable one. Users of an immutable reference don’t expect the values to suddenly change out from under them! Multiple immutable references are okay, however. -## Dangling references +### Dangling references In languages with pointers, it’s easy to create a “dangling pointer” by freeing some memory while keeping around a pointer to that memory. In Rust, by @@ -701,12 +708,17 @@ fn dangle() -> &String { Here’s the error: ```text -error: missing lifetime specifier [E0106] -fn dangle() -> &String { - ^~~~~~~ -help: this function’s return type contains a borrowed value, but there is no - value for it to be borrowed from -help: consider giving it a ‘static lifetime +error[E0106]: missing lifetime specifier + --> dangle.rs:5:16 + | +5 | fn dangle() -> &String { + | ^^^^^^^ + | + = help: this function's return type contains a borrowed value, but there is no + value for it to be borrowed from + = help: consider giving it a 'static lifetime + +error: aborting due to previous error ``` This error message refers to a feature we haven’t learned about yet, @@ -747,26 +759,25 @@ fn no_dangle() -> String { This works, no problem. Ownership is moved out, nothing is deallocated. -## The Rules of References +### The Rules of References -Here’s a recap of what we’ve talked about. The Rules of References: +Here’s a recap of what we’ve talked about: 1. At any given time, you may have _either_, but not both of: 1. One mutable reference. - 2. Any number of immutable references . + 2. Any number of immutable references. 2. References must always be valid. -While these rules are not complicated on their own, they can be tricky when -applied to real code. +Next, let's look at a different kind of reference: slices. -# Slices +## Slices So far, we’ve talked about types that have ownership, like `String`, and ones -that don’t, like `&String`. There is a second kind of type which does not have +that don’t, like `&String`. There is another kind of type which does not have ownership: slices. Slices let you reference a contiguous sequence of elements -in a collection, rather than the whole collection itself. +in a collection rather than the whole collection itself. -Here’s a small programming problem: write a function which takes a string, +Here’s a small programming problem: write a function which takes a string and returns the first word you find. If we don’t find a space in the string, then the whole string is a word, so the whole thing should be returned. @@ -785,8 +796,8 @@ the word, though. Let’s try that: fn first_word(s: &String) -> usize { let bytes = s.as_bytes(); - for (i, &byte) in bytes.iter().enumerate() { - if byte == 32 { + for (i, &item) in bytes.iter().enumerate() { + if item == 32 { return i; } } @@ -797,66 +808,68 @@ fn first_word(s: &String) -> usize { Let’s break that down a bit: -```rust -fn first_word(s: &String) -> usize { - - // Since we need to go through the String element by element, and - // check if a value is a space, we will convert our String to an - // array of bytes, using the `.as_bytes()` method. - let bytes = s.as_bytes(); - - // We discussed using the iter() method with for in Chapter 3.7. Here, - // we’re adding another method: enumerate(). While iter() returns each - // element, enumerate() modifies the result of iter(), and returns a - // tuple instead. The first element of the tuple is the index, and the - // second element is a reference to the element itself. This is a bit - // nicer than calculating the index ourselves. - // - // Since it’s a tuple, we can use patterns, just like elsewhere in Rust. - // So we match against the tuple with i for the index, and &byte for - // the byte itself. - for (i, &byte) in bytes.iter().enumerate() { - - // 32 is the value of a space in UTF-8 - if byte == 32 { - - // We found a space! Return this position. - return i; - } - } - - // If we got here, we didn’t find a space, so this whole thing must be a - // word. So return the length. - s.len() -} +```rust,ignore +let bytes = s.as_bytes(); ``` +Since we need to go through the String element by element and +check if a value is a space, we will convert our String to an +array of bytes using the `.as_bytes()` method. + +```rust,ignore +for (i, &item) in bytes.iter().enumerate() { +``` + +We will be discussing iterators in more detail in Chapter XX, but for +now, know that `iter()` is a method that returns each element in a +collection, and `enumerate()` modifies the result of `iter()` and returns +a tuple instead. The first element of the tuple is the index, and the +second element is a reference to the element itself. This is a bit +nicer than calculating the index ourselves. + +Since it’s a tuple, we can use patterns, just like elsewhere in Rust. So we +match against the tuple with i for the index and &item for a single byte. Since +we get a reference from `.iter().enumerate()`, we use `&` in the pattern. + +```rust,ignore + if item == 32 { + return i; + } +} +s.len() +``` + +We search for the value 32, which represents a space in UTF-8. If we find one, +we return the position. Otherwise, we return the length of the string, using +`s.len()`. + This works, but there’s a problem. We’re returning a `usize` on its own, but -it’s only a meaningful number in the context of the `&String` itself. In other +it’s only a meaningful number in the context of the `&String`. In other words, because it’s a separate value from the `String`, there’s no guarantee that it will still be valid in the future. Consider this: ```rust # fn first_word(s: &String) -> usize { # let bytes = s.as_bytes(); -# -# for (i, &byte) in bytes.iter().enumerate() { -# if byte == 32 { +# +# for (i, &item) in bytes.iter().enumerate() { +# if item == 32 { # return i; # } # } -# +# # s.len() # } fn main() { let mut s = String::from("hello world"); - let word = first_word(&s); + let word = first_word(&s); // word will get the value 5. s.clear(); // This empties the String, making it equal to "". - // word is now totally invalid! There’s no more word here. + // word still has the value 5 here, but there's no more string that + // we could meaningfully use the value 5 with. word is now totally invalid! } ``` @@ -867,13 +880,13 @@ function. Its signature would have to look like this: fn second_word(s: &String) -> (usize, usize) { ``` -Now we’re tracking both a start _and_ and ending index. Even more chances for +Now we’re tracking both a start _and_ an ending index. Even more chances for things to go wrong. We now have three unrelated variable bindings floating around which need to be kept in sync. -Luckily, Rust has a solution to this probem: string slices. +Luckily, Rust has a solution to this problem: string slices. -# String slices +## String slices A string slice looks like this: @@ -881,19 +894,25 @@ A string slice looks like this: let s = String::from("hello world"); let hello = &s[0..5]; -let world = &s[5..9]; +let world = &s[6..11]; ``` This looks just like taking a reference to the whole `String`, but with the -extra `[0..5]` bit. Instead of being a reference to the entire `String`, -it’s a reference to an internal position in the `String`, but it also keeps -track of the number of elements that it refers to as well. In other words, -it looks like this: +extra `[0..5]` bit. Instead of being a reference to the entire `String`, it’s a +reference to an internal position in the `String` and the number of elements +that it refers to. + +We can create slices with a range of `[starting_index..ending_index]`, but the +slice data structure actually stores the starting position and the length of the +slice. So in the case of `let world = &s[6..11];`, `world` would be a slice that +contains a pointer to the 6th byte of `s` and a length value of 5. + +In other words, it looks like this: DIAGRAM GOES HERE of s, hello, and world -With Rust’s `..` syntax, if you want to start at zero, you can drop the zero. -In other words, these are equal: +With Rust’s `..` range syntax, if you want to start at the first index (zero), +you can drop the value before the `..`. In other words, these are equal: ```rust let s = String::from("hello"); @@ -902,8 +921,8 @@ let slice = &s[0..2]; let slice = &s[..2]; ``` -By the same token, if you want to go to the maximum value, which for slices is -the last element, you can drop the trailing number. In other words, these are +By the same token, if your slice should include the last byte of the +`String`, you can drop the trailing number. That means these are equal: ```rust @@ -911,8 +930,20 @@ let s = String::from("hello"); let len = s.len(); -let slice = &s[1..len]; -let slice = &s[1..]; +let slice = &s[3..len]; +let slice = &s[3..]; +``` + +You can also drop both values to take a slice of the entire string. So these +are equal: + +```rust +let s = String::from("hello"); + +let len = s.len(); + +let slice = &s[0..len]; +let slice = &s[..]; ``` With this in mind, let’s re-write `first_word()` to return a slice: @@ -921,8 +952,8 @@ With this in mind, let’s re-write `first_word()` to return a slice: fn first_word(s: &String) -> &str { let bytes = s.as_bytes(); - for (i, &byte) in bytes.iter().enumerate() { - if byte == 32 { + for (i, &item) in bytes.iter().enumerate() { + if item == 32 { return &s[0..i]; } } @@ -931,31 +962,22 @@ fn first_word(s: &String) -> &str { } ``` -Now, we have a single value, the `&str`. It contains both elements that we care -about: a reference to the starting point, and the number of elements. +Now we have a single value, the `&str`, pronounced "string slice". It stores +both elements that we care about: a reference to the starting point of the +slice and the number of elements in the slice. + This would also work for a `second_word()`: ```rust,ignore fn second_word(s: &String) -> &str { ``` -Same deal. We now have a straightforward API, that’s much harder to mess up. +We now have a straightforward API that’s much harder to mess up. But what about our error condition from before? Slices also fix that. Using the slice version of `first_word()` will throw an error: ```rust,ignore -# fn first_word(s: &String) -> &str { -# let bytes = s.as_bytes(); -# -# for (i, &byte) in bytes.iter().enumerate() { -# if byte == 32 { -# return &s[0..i]; -# } -# } -# -# &s[..] -# } fn main() { let mut s = String::from("hello world"); @@ -967,7 +989,7 @@ fn main() { Here’s the error: -```text +```bash 17:6 error: cannot borrow `s` as mutable because it is also borrowed as immutable [E0502] s.clear(); // Error! @@ -1003,7 +1025,7 @@ The type of `s` here is `&str`: It’s a slice, pointing to that specific point of the binary. This is also why string literals are immutable; `&str` is an immutable reference. -## String slices as arguments +### String slices as arguments Knowing that you can take slices of both literals and `String`s leads us to one more improvement on `first_word()`, and that’s its signature: @@ -1027,27 +1049,33 @@ with no loss of functionality: ```rust # fn first_word(s: &str) -> &str { # let bytes = s.as_bytes(); -# -# for (i, &byte) in bytes.iter().enumerate() { -# if byte == 32 { +# +# for (i, &item) in bytes.iter().enumerate() { +# if item == 32 { # return &s[0..i]; # } # } -# +# # &s[..] # } fn main() { - let s = String::from("hello world"); - let word = first_word(&s[..]); + let my_string = String::from("hello world"); - let s = "hello world"; - let word = first_word(&s[..]); + // first_word works on slices of `String`s + let word = first_word(&my_string[..]); - let word = first_word(s); // since literals are &strs, this works too! + let my_string_literal = "hello world"; + + // first_word works on slices of string literals + let word = first_word(&my_string_literal[..]); + + // since string literals *are* string slices already, + // this works too, without the slice syntax! + let word = first_word(my_string_literal); } ``` -# Other slices +## Other slices String slices, as you might imagine, are specific to strings. But there’s a more general slice type, too. Consider arrays: @@ -1066,6 +1094,6 @@ let slice = &a[1..3]; ``` This slice has the type `&[i32]`. It works the exact same way as string slices -do, with a reference to the first element, and a length. You’ll use this kind -of slice for all sorts of other collections. We’ll discuss these other slices -in detail when we talk about vectors, in Chapter 9.1. +do, by storing a reference to the first element and a length. You’ll use this +kind of slice for all sorts of other collections. We’ll discuss these in detail +when we talk about vectors in Chapter XX. diff --git a/nostarch/chapter05.md b/nostarch/chapter05.md new file mode 100644 index 000000000..23ca0a003 --- /dev/null +++ b/nostarch/chapter05.md @@ -0,0 +1,486 @@ +# Structs + +A `struct`, short for "structure", gives us the ability to name and package +together multiple related values that make up a meaningful group. If you come +from an object-oriented language, a `struct` is like an object's data +attributes. `struct` and `enum` (that we talked about in the last chapter) are +the building blocks you can use in Rust to create new types in your program's +domain in order to take full advantage of Rust's compile-time type checking. + +Let’s write a program which calculates the distance between two points. +We’ll start off with single variable bindings, and then refactor it to +use `struct`s instead. + +Let’s make a new project with Cargo: + +```bash +$ cargo new --bin points +$ cd points +``` + +Here’s a short program which calculates the distance between two points. Put +it into your `src/main.rs`: + +```rust +fn main() { + let x1 = 0.0; + let y1 = 5.0; + + let x2 = 12.0; + let y2 = 0.0; + + let answer = distance(x1, y1, x2, y2); + + println!("Point 1: ({}, {})", x1, y1); + println!("Point 2: ({}, {})", x2, y2); + println!("Distance: {}", answer); +} + +fn distance(x1: f64, y1: f64, x2: f64, y2: f64) -> f64 { + let x_squared = f64::powi(x2 - x1, 2); + let y_squared = f64::powi(y2 - y1, 2); + + f64::sqrt(x_squared + y_squared) +} +``` + +Let's try running this program with `cargo run`: + +```bash +$ cargo run + Compiling points v0.1.0 (file:///projects/points) + Running `target/debug/points` +Point 1: (0, 5) +Point 2: (12, 0) +Distance: 13 +``` + +Let's take a quick look at `distance()` before we move forward. To find the +distance between two points, we can use the Pythagorean Theorem. The theorem is +named after Pythagoras, who was the first person to mathematically prove this +formula. The details aren't that important; just know the theorem says that the +formula for the distance between two points is equal to: + +- squaring the distance between the points horizontally (the "x" direction) +- squaring the distance between the points vertically (the "y" direction) +- adding those together +- and taking the square root of that. + +So that's what we're implementing here. + +```rust,ignore +f64::powi(2.0, 3) +``` + +The double colon (`::`) here is a namespace operator. We haven’t talked about +modules and namespaces in depth yet, but you can think of the `powi()` function +as being scoped inside of another name. In this case, the name is `f64`, the +same as the type. The `powi()` function takes two arguments: the first is a +number, and the second is the power that it raises that number to. In this +case, the second number is an integer, hence the ‘i’ in its name. Similarly, +`sqrt()` is a function under the `f64` module, which takes the square root of +its argument. + +## Why `struct`s? + +Our little program is okay, but we can do better. The key to seeing this is in +the signature of `distance()`: + +```rust,ignore +fn distance(x1: f64, y1: f64, x2: f64, y2: f64) -> f64 { +``` + +The distance function is supposed to calculate the distance between two points. +But our distance function calculates some distance between four numbers. The +first two and last two arguments are related, but that’s not expressed anywhere +in our program itself. It would be nicer if we had a way to group `(x1, y1)` +and `(x2, y2)` together. + +We’ve already discussed one way to do that: tuples. Here’s a version of our +program which uses tuples: + +```rust +fn main() { + let p1 = (0.0, 5.0); + + let p2 = (12.0, 0.0); + + let answer = distance(p1, p2); + + println!("Point 1: {:?}", p1); + println!("Point 2: {:?}", p2); + println!("Distance: {}", answer); +} + +fn distance(p1: (f64, f64), p2: (f64, f64)) -> f64 { + let x_squared = f64::powi(p2.0 - p1.0, 2); + let y_squared = f64::powi(p2.1 - p1.1, 2); + + f64::sqrt(x_squared + y_squared) +} +``` + +This is a little better, for sure. Tuples let us add a little bit of structure. +We’re now passing two arguments, so that’s more clear. But it’s also worse: +tuples don’t give names to their elements, so our calculation has gotten more +confusing: + +```rust,ignore +p2.0 - p1.0 +p2.1 - p1.1 +``` + +When writing this example, your authors almost got it wrong themselves! Distance +is all about `x` and `y` points, but our code is talking about `0` and `1`. +This isn’t great. + +Enter `struct`s. We can transform our tuples into something with a name for the +whole as well as names for the parts: + +```rust,ignore +let p1 = (0.0, 5.0); + +struct Point { + x: f64, + y: f64, +} + +let p1 = Point { x: 0.0, y: 5.0 }; +``` + +Here we've defined a `struct` and given it the name `Point`. The parts inside +`{}` are defining the _fields_ of the struct. We can have as many or as few of +them as we'd like, and we give them a name and specify their type. Here we have +two fields named `x` and `y`, and they both hold `f64`s. + +We can access the field of a struct in the same way we access an element of +a tuple, except we use its name: + +```rust,ignore +let p1 = (0.0, 5.0); +let x = p1.0; + +struct Point { + x: f64, + y: f64, +} + +let p1 = Point { x: 0.0, y: 5.0 }; +let x = p1.x; +``` + +Let’s convert our program to use our `Point` `struct`. Here’s what it looks +like now: + +```rust +#[derive(Debug,Copy,Clone)] +struct Point { + x: f64, + y: f64, +} + +fn main() { + let p1 = Point { x: 0.0, y: 5.0}; + + let p2 = Point { x: 12.0, y: 0.0}; + + let answer = distance(p1, p2); + + println!("Point 1: {:?}", p1); + println!("Point 2: {:?}", p2); + println!("Distance: {}", answer); +} + +fn distance(p1: Point, p2: Point) -> f64 { + let x_squared = f64::powi(p2.x - p1.x, 2); + let y_squared = f64::powi(p2.y - p1.y, 2); + + f64::sqrt(x_squared + y_squared) +} +``` + +Our function signature for `distance()` now says exactly what we mean: it +calculates the distance between two `Point`s. And rather than `0` and `1`, +we’ve got back our `x` and `y`. This is a win for clarity. + +## Derived Traits + +There’s one other thing that’s a bit strange here, this stuff above the +`struct` declaration: + +```rust,ignore +#[derive(Debug,Copy,Clone)] +struct Point { +``` + +This is an annotation that tells the compiler our struct should get some +default behavior for the `Debug`, `Copy`, and `Clone` traits. We talked about +marking that types can be `Copy` and `Clone`-able in Chapter XX when we +discussed ownership. `Debug` is the trait that enables us to print out our +struct so that we can see its value while we are debugging our code. + +So far, we’ve been printing values using `{}` in a `println!` macro. If we try +that with a struct, however, by default, we'll get an error. Say we have the +following program: + +```rust,ignore +struct Point { + x: f64, + y: f64, +} + +fn main() { + let p1 = Point { x: 0.0, y: 5.0}; + println!("Point 1: {}", p1); +} +``` + +This code tries to print the `p1` point directly, which may seem innocuous. But +running it produces the following output: + +```bash +$ cargo run + Compiling points v0.1.0 (file:///projects/points) +error: the trait bound `Point: std::fmt::Display` is not satisfied [--explain E0277] + --> src/main.rs:8:29 +8 |> println!("Point 1: {}", p1); + |> ^^ +:2:27: 2:58: note: in this expansion of format_args! +:3:1: 3:54: note: in this expansion of print! (defined in ) +src/main.rs:8:5: 8:33: note: in this expansion of println! (defined in ) +note: `Point` cannot be formatted with the default formatter; try using `:?` instead if you are using a format string +note: required by `std::fmt::Display::fmt` +``` + +Whew! The core of the error is this part: *the trait bound `Point: +std::fmt::Display` is not satisfied*. `println!` can do many kinds of +formatting. By default, `{}` implements a kind of formatting known as +`Display`: output intended for direct end-user consumption. The primitive types +we’ve seen implement `Display`, as there’s only one way you’d show a `1` to a +user. But with structs, the output is less clear. Do you want commas or not? +What about the `{}`s? Should all the fields be shown? + +More complex types in the standard library and that are defined by the +programmer do not automatically implement `Display` formatting. Standard +library types implement `Debug` formatting, which is intended for the +programmer to see. The `#[derive(Debug)]` annotation lets us use a default +implementation of `Debug` formatting to easily get this ability for types we've +defined. To ask `println!` to use `Debug` formatting with our `Point`, we add +the annotation to derive the trait and include `:?` in the print string, like +this: + +```rust +#[derive(Debug)] +struct Point { + x: f64, + y: f64, +} + +fn main() { + let p1 = Point { x: 0.0, y: 5.0}; + println!("Point 1: {:?}", p1); +} +``` + +If you run this, it should print the values of each field in the `Point` struct +as desired: + +```bash +$ cargo run + Compiling points v0.1.0 (file:///projects/points) + Running `target/debug/points` +Point 1: Point { x: 0, y: 5 } +``` + +You’ll see this repeated later with other types. We’ll cover traits fully in +Chapter XX. + +## Method Syntax + +In the last section on ownership, we made several references to ‘methods’. +Methods look like this: + +```rust +let s1 = "hello"; + +// call a method on s1 +let s2 = s1.clone(); + +println!("{}", s1); +``` + +The call to `clone()` is attached to `s1` with a dot. This is called ‘method +syntax’, and it’s a way to call certain functions with a different style. + +Why have two ways to call functions? We’ll talk about some deeper reasons +related to ownership in a moment, but one big reason is that methods look nicer +when chained together: + +```rust,ignore +// with functions +h(g(f(x))); + +// with methods +x.f().g().h(); +``` + +The nested-functions version reads in reverse: the program executes `f()`, then +`g()`, then `h()`, but we read it left-to-right as `h()`, then `g()`, then +`f()`. The method syntax is executed in the same order as we would read it. + +Before we get into the details, let’s talk about how to define your own +methods. + +### Defining methods + +We can define methods with the `impl` keyword. `impl` is short for +‘implementation’. Doing so looks like this: + +```rust +#[derive(Debug,Copy,Clone)] +struct Point { + x: f64, + y: f64, +} + +impl Point { + fn distance(&self, other: &Point) -> f64 { + let x_squared = f64::powi(other.x - self.x, 2); + let y_squared = f64::powi(other.y - self.y, 2); + + f64::sqrt(x_squared + y_squared) + } +} + +let p1 = Point { x: 0.0, y: 0.0 }; +let p2 = Point { x: 5.0, y: 6.5 }; + +assert_eq!(8.200609733428363, p1.distance(&p2)); +``` + +Let’s break this down. First, we have our `Point` struct from earlier in the +chapter. Next comes our first use of the `impl` keyword: + +```rust,ignore +impl Point { + // ... +} +``` + +Everything we put inside of the curly braces will be methods implemented on +`Point`. Next is our definition: + +```rust,ignore +fn distance(&self, other: &Point) -> f64 { + // ... +} +``` + +Other than this, the rest of the example is familiar: an implementation of +`distance()` and using the method to find an answer. + +Our definition of `distance()` here as a method looks very similar to our +previous definition of `distance()` as a function, but with two differences. +Here's the `distance()` function again: + +```rust,ignore +fn distance(p1: Point, p2: Point) -> f64 { + // ... +} +``` + +The first difference is in the first argument. Instead of a name and a type, we +have written `&self`. This is what distinguishes a method from a function: +using `self` inside of an `impl` block means we have a method. Because we +already know that we are implementing this method on `Point` because of the +surrounding `impl Point` block, we don’t need to write the type of `self` out. + +Note that we have written `&self`, not just `self`. This is because we want to +take a reference to our argument's value rather than taking ownership of it. In +other words, these two forms are the same: + +```rust,ignore +fn foo(self: &Point) +fn foo(&self) +``` + +Just like any other parameter, you can take `self` in three forms. Here’s the +list, with the most common form first: + +```rust,ignore +fn foo(&self) // take self by reference +fn foo(&mut self) // take self by mutable reference +fn foo(self) // take self by ownership +``` + +In this case, we only need a reference. We don’t need to mutate either `Point` +to get the distance between them, so we won't take a mutable reference to the +`Point` that we call the method on. Methods that take ownership of `self` are +rarely used. An example of a time to do that would be if we wanted to have a +method that would transform `self` into something else and prevent other code +from using the value of `self` after the transformation happens. + +#### Methods and automatic referencing + +We’ve left out an important detail. It’s in this line of the example: + +```rust,ignore +assert_eq!(8.200609733428363, p1.distance(&p2)); +``` + +When we defined `distance()`, we took both `self` and the other argument by +reference. Yet, we needed a `&` for `p2` but not `p1`. What gives? + +This feature is called ‘automatic referencing’, and calling methods is one +of the few places in Rust that has behavior like this. Here’s how it works: +when you call a method with `self.(`, Rust will automatically add in `&`s +or `&mut`s to match the signature. In other words, these are the same: + +```rust +# #[derive(Debug,Copy,Clone)] +# struct Point { +# x: f64, +# y: f64, +# } +# +# impl Point { +# fn distance(&self, other: &Point) -> f64 { +# let x_squared = f64::powi(other.x - self.x, 2); +# let y_squared = f64::powi(other.y - self.y, 2); +# +# f64::sqrt(x_squared + y_squared) +# } +# } +# let p1 = Point { x: 0.0, y: 0.0 }; +# let p2 = Point { x: 5.0, y: 6.5 }; +p1.distance(&p2); +(&p1).distance(&p2); +``` + +The first one looks much, much cleaner. Here’s another example: + +```rust +let mut s = String::from("Hello,"); + +s.push_str(" world!"); + +// The above is the same as: +// (&mut s).push_str(" world!"); + +assert_eq!("Hello, world!", s); +``` + +Because [`push_str()`] has the following signature: + +```rust,ignore +fn push_str(&mut self, string: &str) { +``` + +[`push_str()`]: http://doc.rust-lang.org/collections/string/struct.String.html#method.push_str + +This automatic referencing behavior works because methods have a clear receiver +— the type of `self` — and in most cases it’s clear given the receiver and name +of a method whether the method is just reading (so needs `&self`), mutating (so +`&mut self`), or consuming (so `self`). The fact that Rust makes borrowing +implicit for method receivers is a big part of making ownership ergonomic in +practice. diff --git a/nostarch/chapter06.md b/nostarch/chapter06.md new file mode 100644 index 000000000..57d5ba53d --- /dev/null +++ b/nostarch/chapter06.md @@ -0,0 +1,601 @@ +# Enums + +Next, let’s look at *enumerations*, which allow you to define a type by +enumerating its possible values. Commonly called "enums", these unlock a lot of +power in Rust when combined with pattern matching. Enums are a feature that are +in many languages, but what they can do is different per-language. Rust’s enums +are most similar to "algebraic data types" in functional languages like F#, +OCaml, or Haskell. + +Here’s an example of an enum definition: + +```rust +enum IpAddrKind { + V4, + V6, +} +``` + +This enum represents the kind of an IP address. There are two major standards +used for IP addresses: version four and version six. Any IP address can be +either a version four address or a version six address, but it cannot be both +kinds at the same time. This is where enums get their name: they allow us to +enumerate all of the possible kinds that our value can have. + +We can create values of `IpAddrKind` like this: + +```rust +# enum IpAddrKind { +# V4, +# V6, +# } +# +let four = IpAddrKind::V4; +let six = IpAddrKind::V6; +``` + +Note that the variants of the enum are namespaced under its name, and we use +the double colon to separate the two. + +Enums have more tricks up their sleeves, however. Thinking more about our IP +address type, we don’t have a way to store the actual data of the IP address; +we only know what kind it is. Given that you just learned about structs, you +might tackle this problem like this: + +```rust +enum IpAddrKind { + V4, + V6, +} + +struct IpAddr { + kind: IpAddrKind, + address: String, +} + +let home = IpAddr { + kind: IpAddrKind::V4, + address: String::from("127.0.0.1"), +}; + +let loopback = IpAddr { + kind: IpAddrKind::V6, + address: String::from("::1"), +}; +``` + +We’ve used a struct to bundle the two values together: now we keep the kind +with the value itself. We can represent the same thing in a different way with +just an enum: + +```rust +enum IpAddr { + V4(String), + V6(String), +} + +let home = IpAddr::V4(String::from("127.0.0.1")); + +let loopback = IpAddr::V6(String::from("::1")); +``` + +We can attach data to each variant of the enum directly. No need for an extra +struct. But beyond that, this approach is better than using a struct alongside +our enum because we can attach different kinds of data to each variant. +Imagine that instead of a `String`, we would prefer to store a `V4` as its four +individual components while leaving the `V6` variant as a `String`. With our +struct, we’d be stuck. But enums deal with this case with ease: + +```rust +enum IpAddr { + V4(u8, u8, u8, u8), + V6(String), +} + +let home = IpAddr::V4(127, 0, 0, 1); + +let loopback = IpAddr::V6(String::from("::1")); +``` + +You can put any kind of data inside of an enum variant, including another enum! +The `IpAddr` enum is [in the standard library][IpAddr], but it embeds two +different structs inside of its variants: + +```rust +struct Ipv4Addr { + // details elided +} + +struct Ipv6Addr { + // details elided +} + +enum IpAddr { + V4(Ipv4Addr), + V6(Ipv6Addr), +} +``` + +[IpAddr]: http://doc.rust-lang.org/std/net/enum.IpAddr.html + +Here’s an enum with a variety of types embedded in its variants: + +```rust +enum Message { + Quit, + Move { x: i32, y: i32 }, + Write(String), + ChangeColor(i32, i32, i32), +} +``` + +* `Quit` has no data associated with it at all. +* `Move` includes an anonymous struct inside of it. +* `Write` includes a single `String`. +* `ChangeColor` includes three `i32`s. + +This might seem overwhelming, but another way to look at the different enum +possibilities is that they are just like different kinds of struct definitions +that you already know, except without the `struct` keyword and they are grouped +together under the `Message` type. These structs could hold the same data that +these enum variants hold: + +``` +struct QuitMessage; // unit struct +struct MoveMessage { + x: i32, + y: i32, +} +struct WriteMessage(String); // tuple struct +struct ChangeColorMessage(i32, i32, i32); // tuple struct +``` + +Let's look at another enum in the standard library that is very common and +useful: `Option`. + +## Option + +Now that we have had an introduction to enums, let's combine them with a +feature that we talked a little bit about in the previous chapter: generics. + +Programming language design is often thought of as which features you include, +but it's also about which features you leave out. Rust does not have a feature +that is in many other languages: 'null'. In languages with this feature, +variables can have two states: null or not-null. + +The inventor of this concept has this to say: + +> I call it my billion-dollar mistake. At that time, I was designing the first +> comprehensive type system for references in an object-oriented language. My +> goal was to ensure that all use of references should be absolutely safe, with +> checking performed automatically by the compiler. But I couldn't resist the +> temptation to put in a null reference, simply because it was so easy to +> implement. This has led to innumerable errors, vulnerabilities, and system +> crashes, which have probably caused a billion dollars of pain and damage in +> the last forty years. +> +> - Tony Hoare "Null References: The Billion Dollar Mistake" + +The problem with null values is twofold: first, a value can be null or not, at +any time. The second is that if you try to use a value that's null, you'll get +an error of some kind, depending on the language. Because this property is +pervasive, it's extremely easy to make this kind of error. + +Even with these problems, the concept that null is trying to express is still a +useful one: this is a value which is currently invalid or not present for some +reason. The problem isn't with the concept itself, but with the particular +implementation. As such, Rust does not have the concept of null, but we do have +an enum which can encode the concept of a value being present or not present. We +call this enum `Option`, and it looks like this: + +```rust +enum Option { + Some(T), + None, +} +``` + +This enum is [provided by the standard library][option], and is so useful that +it's even in the prelude; you don't need to import it explicitly. Furthermore, +so are its variants: you can say `Some` and `None` directly, without prefixing +them with `Option::`. + +[option]: ../std/option/enum.Option.html + +Here's an example of using `Option`: + +```rust +let some_number = Some(5); +let some_string = Some("a string"); + +// If we only say None, we need to tell Rust what type of Option we have. +let absent_number: Option = None; +``` + +Let's dig in. First, you'll notice that we used the `` syntax when defining +`Option`: it's a generic enum. `Option` has two variants: `Some`, which +contains a `T`, and `None`, which has no data associated with it. In some +sense, `None` means 'null', and `Some` means 'not null'. So why is this any +better than null? + +In short, because `Option` and `T` are different types. That's a bit too +short though. Here's an example: + +```rust,ignore +let x: i8 = 5; +let y: Option = Some(5); + +let sum = x + y; +``` + +This will not compile. We get an error message like this: + +```text +error: the trait bound `i8: std::ops::Add>` is not +satisfied [E0277] + +let sum = x + y; + ^~~~~ +``` + +Intense! What this error message is trying to say is that Rust does not +understand how to add an `Option` and a `T`. They're different types! This +shows one of the big advantages of an `Option`: if you have a value that +may or may not exist, you have to deal with that fact before you can assume it +exists. In other words, you have to convert an `Option` to a `T` before you +can do `T` stuff with it. This helps catch one of the most common issues with +null, generally: assuming that something isn't null when it actually is. + +This is pretty powerful: in order to have a value that can possibly be null, +you have to explicitly opt in by making the type of that value an `Option`. +Then, when you use that value, you are required to explicitly handle the case +when the value is null. Everywhere that a value has a type that isn't an +`Option`, you *can* safely assume that the value isn't null. This was a +deliberate design decision for Rust to limit null's pervasiveness and increase +the safety of Rust code. + +So, how _do_ you get a `T` from an `Option`? The `Option` enum has a +large number of methods that you can check out in [its documentation], and +becoming familiar with them will be extremely useful in your journey with Rust. + +[its documentation]: ../std/option/enum.Option.html + +But we want a deeper understanding than that. If we didn't have those methods +defined for us already, what would we do? And more generally, how do we get +the inner values out of any enum variant? We need a new feature: `match`. + +## Match + +Rust has an extremely powerful control-flow operator: `match`. It allows us to +compare a value against a series of patterns and then execute code based on +how they compare. + +Think of a `match` expression kind of like a coin sorting machine. Coins slide +down a track that has variously sized holes along it, and each coin falls +through the first hole it encounters that it fits into. In the same way, values +go through each pattern in a `match`, and for the first pattern that the value +"fits", the value will fall into the associated code block to be used during +execution. + +Since we're already talking about coins, let's use them for an example using +`match`! We can write a function that can take an unknown American coin and, in +a similar way as the coin counting machine, determine which coin it is and +return its value in cents: + +```rust +enum Coin { + Penny, + Nickel, + Dime, + Quarter, +} + +fn value_in_cents(coin: Coin) -> i32 { + match coin { + Coin::Penny => 1, + Coin::Nickel => 5, + Coin::Dime => 10, + Coin::Quarter => 25, + } +} +``` + +Let's break down the `match`! At a high-level, using `match` looks like this: + +```text +match expression { + pattern => code, +} +``` + +First, we have the `match` keyword. Next, we have an expression. This feels +very similar to an expression used with `if`, but there's a big difference: +with `if`, the condition needs to return a boolean value. Here, it can be any +type. + +Next, we have a "match arm". That's the part that looks like `pattern => +code,`. We can have as many arms as we need to: our `match` above has four +arms. An arm has two parts: a pattern and some code. When the `match` +expression executes, it compares the resulting value against the pattern of +each arm, in order. If a pattern matches the value, the code associated +with that pattern is executed. If that pattern doesn't match the value, +execution continues to the next arm, much like a coin sorting machine. + +The code associated with each arm is an expression, and the resulting value of +the code with the matching arm that gets executed is the value that gets +returned for the entire `match` expression. + +Curly braces typically aren't used if the match arm code is short, as it is in +the above example where each arm just returns a value. If we wanted to run +multiple lines of code in a match arm, we can use curly braces. This code would +print out "Lucky penny!" every time the method was called with a `Coin::Penny`, +but would still return the last value of the block, `1`: + +```rust +# enum Coin { +# Penny, +# Nickel, +# Dime, +# Quarter, +# } +# +fn value_in_cents(coin: Coin) -> i32 { + match coin { + Coin::Penny => { + println!("Lucky penny!"); + 1 + }, + Coin::Nickel => 5, + Coin::Dime => 10, + Coin::Quarter => 25, + } +} +``` + +Another useful feature of match arms is that they can create bindings to parts +of the values that match the pattern. From 1999 through 2008, the U.S. printed +quarters with different designs for each of the 50 states on one side. The other +coins did not get state designs, so only quarters have this extra attribute. We +can add this information to our `enum` by changing the `Quarter` variant to have +a `State` value: + +```rust +enum UsState { + Alabama, + Alaska, + // ... etc +} + +enum Coin { + Penny, + Nickel, + Dime, + Quarter(UsState), +} +``` + +Let's imagine that a friend of ours is trying to collect all 50 state quarters. +While we sort our loose change by coin type in order to count it, we're going +to call out the name of the state so that if it's one our friend doesn't have +yet, they can add it to their collection. + +In the match statement to do this, the quarter case now has a binding, `state`, +that contains the value of the state of that quarter. The binding will only get +created if the coin matches the `Quarter` pattern. Then we can use the binding +in the code for that arm: + +```rust +# #[derive(Debug)] +# enum UsState { +# Alabama, +# Alaska, +# } +# +# enum Coin { +# Penny, +# Nickel, +# Dime, +# Quarter(UsState), +# } +# +fn value_in_cents(coin: Coin) -> i32 { + match coin { + Coin::Penny => 1, + Coin::Nickel => 5, + Coin::Dime => 10, + Coin::Quarter(state) => { + println!("State quarter from {:?}!", state); + 25 + }, + } +} +``` + +If we were to call `value_in_cents(Coin::Quarter(UsState::Alaska))`, `coin` will +be `Coin::Quarter(UsState::Alaska)`. When we compare that value with each of the +match arms, none of them match until we reach `Coin::Quarter(state)`. At that +point, the binding for `state` will be the value `UsState::Alaska`. We can then +use that binding in the `println!`, thus getting the inner state value out of +the `Coin` enum variant for `Quarter`. + +Remember the `Option` type from the previous section, and that we wanted to +be able to get the inner `T` value out of the `Some` case? This will be very +similar! Instead of coins, we will be comparing to other patterns, but the way +that the `match` expression works remains the same as a coin sorting machine in +the way that we look for the first pattern that fits the value. + +Let's say that we want to write a function that takes an `Option`, and +if there's a value inside, add one to it. If there isn't a value inside, we +want to return the `None` value and not attempt to add. + +This function is very easy to write, thanks to `match`. It looks like this: + +```rust +fn plus_one(x: Option) -> Option { + match x { + None => None, + Some(i) => Some(i + 1), + } +} + +let five = Some(5); +let six = plus_one(five); +let none = plus_one(None); +``` + +Let's examine the first execution of `plus_one()` in more detail. In the above +example, `x` will be `Some(5)`. Let's compare that against each arm: + +```text +None => None, +``` + +Does `Some(5)` match `None`? No, it's the wrong variant. So let's continue. + +```text +Some(i) => Some(i + 1), +``` + +Does `Some(5)` match `Some(i)`? Why yes it does! We have the same variant. The +`i` binds to the value inside of the `Some`, so `i` has the value `5`. Then we +execute the code in that match arm: take `i`, which is `5`, add one to it, and +create a new `Some` value with our total inside. + +Now let's consider the second call of `plus_one()`. In this case, `x` is +`None`. We enter the `match`, and compare to the first arm: + +```text +None => None, +``` + +Does `None` match `None`? Yup! There's no value to add to. So we stop and +return the `None` value that is on the right side of the `=>`. We don't +check any other arms since we found one that matched. + +Combining `match` and enums together is extremely powerful. You'll see this +pattern a lot in Rust code: `match` against an enum, bind to the data +inside, and then execute code based on it. It's a bit tricky at first, but +once you get used to it, you'll wish you had it in languages that don't support +it. It's consistently a user favorite. + +### Matches are exhaustive + +There's one other aspect of `match` we didn't talk about. Consider this version +of `plus_one()`: + +```rust,ignore +fn plus_one(x: Option) -> Option { + match x { + Some(i) => Some(i + 1), + } +} +``` + +A bug! We didn't handle the `None` case. Luckily, it's a bug Rust knows how to +catch. If we try to compile this code, we'll get an error: + +```text +error: non-exhaustive patterns: `None` not covered [E0004] +match x { + Some(i) => Some(i + 1), +} +``` + +Rust knows that we did not cover every possible option, and even knows which +pattern we forgot! This is referred to as being "exhaustive": we must exhaust +every last option possible in order to be valid. Especially in the case of +`Option`, when Rust prevents us from forgetting to explicitly handle the +`None` case, it protects us from assuming that we have a value when we might +have null and thus making the billion-dollar mistake we discussed in the +previous section. + +### The _ placeholder + +What if we don't care about all of the possible values, though? Especially when +there are a lot of possible values for a type: a `u8` can have valid values of +zero through 255-- if we only care about 1, 3, 5, and 7, does this mean we must +list out 0, 2, 4, 6, 8, 9, all the way up through 255? Thankfully, no! We can +use a special pattern, `_`: + +```rust +let some_u8_value = 0u8; +match some_u8_value { + 1 => println!("one"), + 3 => println!("three"), + 5 => println!("five"), + 7 => println!("seven"), + _ => (), +} +``` + +The `_` pattern will match all the other cases, and `()` will do nothing, it's +the unit value. This way, we don't have to list individual match arms for all +the other possible values in order to say that we want to do nothing for all of +those-- the `_` is a placeholder for any value. + +## if let + +There's one more advanced control flow structure we haven't discussed: `if +let`. Imagine we're in a situation like this: + +```rust +# let some_option = Some(5); +match some_option { + Some(x) => { + // do something with x + }, + None => {}, +} +``` + +We care about the `Some` case, but don't want to do anything with the `None` +case. With an `Option`, this isn't _too_ bad, but with a more complex enum, +adding `_ => {}` after processing just one variant doesn't feel great. We have +this boilerplate arm and an extra level of indentation (the code that +does something with `x` is indented twice, rather than just once). We really want +a construct that says "Do something with this one case; I don't care about the +others." + +Enter `if let`: + +```rust +# let some_option = Some(5); +if let Some(x) = some_option { + // do something with x +} +``` + +`if let` takes a pattern and an expression, separated by an `=`. It works +exactly like a `match`, where the expression is given to the `match` and the +pattern is its first arm. In other words, you can think of `if let` as syntax +sugar: + +```rust,ignore +if let pattern = expression { + body +} + +match expression { + pattern => body, + _ => {} +} +``` + +And in fact, we can include an `else` and it becomes the body of the `_` +case: + +```rust,ignore +if let pattern = expression { + body +} else { + else_body +} + +match expression { + pattern => body, + _ => else_body, +} +``` + +In other words, it's the high-level construct we were originally looking for: +do something special with only one pattern. diff --git a/nostarch/chapter3.md b/nostarch/chapter3.md deleted file mode 100644 index 19581d40b..000000000 --- a/nostarch/chapter3.md +++ /dev/null @@ -1,2332 +0,0 @@ - - - - - - - - - -[TOC] - -# Up and Running - -We’ll start our Rust journey by talking about the absolute basics, concepts that appear in almost every programming language. Many programming languages have much in common at their core. None of the concepts presented in this chapter are unique to Rust, but we’ll discuss Rust’s particular syntax and conventions concerning these common concepts. - - - - - - - - -Specifically, we’ll be talking about variable bindings, functions, basic types, comments, `if` statements, and looping. These foundations will be in every Rust program, and learning them early will give you a strong core to start from. - - -## Anatomy of a Rust Program - - - -The foundation of virtually every program is the ability to store and modify data, but to create this data, you first have to create a program. Here, we'll write some code that demonstrates how to begin a Rust program, how to bind a variable, and how to print text to the terminal. - - - - - -#### A Simple Program that Binds a Variable - - - - -Let’s start with a short example that binds a value to a variable, and then uses that binding in a sentence that we'll print to the screen. First, we’ll generate a new project with Cargo. Open a terminal, and navigate to the directory you want to store your projects in. From there, generate a new project: - -```bash -$ cargo new --bin bindings -$ cd bindings -``` - - - - -This creates a new project called `bindings` and sets up our *Cargo.toml* and *src/main.rs* files. As we saw in Chapter XX, Cargo will generate these files and create a little "hello world" program like this: - -```rust -fn main() { - println!("Hello, world!"); -} -``` - -Open that program and replace its code with the following: - -```rust -fn main() { - let x = 5; - - println!("The value of x is: {}", x); -} -``` - -This is the full program for our example. Enter the `run` command now to to see it working: - -```bash -$ cargo run - Compiling bindings v0.1.0 (file:///projects/bindings) - Running `target/debug/bindings` -The value of x is: 5 -``` - - - - -If you get an error instead of this output, double check that you've copied the program -exactly as written, and then try again. Now let’s break this program down, line by line. - -#### Starting a Program with the main() Function - -Most Rust programs open with the same first line as this one from our example program: - -```rust,ignore -fn main() { -``` - -The `main()` function is the entry point of every Rust program. It doesn’t have to be at the very beginning of our source code, but it will be the first bit of code that runs when we execute our program. We’ll talk more about functions in the next section, but for now, just know is that ```main()``` is where our program begins. The opening curly brace (`{`) indicates the start of the function’s body. - - - - - -#### Binding a Variable with `let` - -Inside the function body, we added the following: - -```rust,ignore - let x = 5; -``` - -This is a `let` statement, and it binds the value `5` to the variable `x`. Basic `let` statements take the following form: - -```text -let NAME = EXPRESSION; -``` - - - - - - -A `let` statement first evaluates the `EXPRESSION`, and then binds the resulting value to `NAME` to give us a variable to use later in the program. Notice the semicolon at the end of the statement, too. As in many other programming languages, statements in Rust must end with a semicolon. - -In this simple example, the expression already is a value, but we could achieve the same result like this: - -```rust -let x = 2 + 3; -``` - - - -The expression `2 + 3` would evaluate to `5`, which would in turn be stored in the `x` variable binding. In general, `let` statements work with *patterns*. Patterns are part of a feature of Rust called ‘pattern matching’. We can compare an expression against a pattern, and then make a choice based on how the two compare. A name like `x` is a particularly humble form of pattern; it will always match. Patterns are a big part of Rust, and we’ll see more complex and powerful patterns as we go along. - - - - -#### Printing to the Screen with a Macro - -The next line of our program is: - -```rust,ignore - println!("The value of x is: {}", x); -``` - - - - -The `println!` command is a *macro* that prints the text passed to it to the screen. Macros are indicated with the `!` character. In Chapter , you'll learn how to write macros your, but for now we'll use macros provided by the standard Rust library. Macros can add new syntax to the language, and the `!` is a reminder that things may look slightly unusual. - - - - -The `println!` macro only requires one argument: a format string. You can add optional arguments inside this format string by using the special text `{}`. Each instance of `{}` corresponds to an additional argument. Here’s an example: - -```rust -let x = 2 + 3; -let y = x + 5; - -println!("The value of x is {}, and the value of y is {}", x, y); -``` - -If you were to run a program containing these statements, it would print the following: - -```rust -The value of x is 5, and the value of y is 10 -``` - -Think of `{}` as little crab pincers, holding a value in place. The first `{}` holds the first value after the format string, the second set holds the second value, and so on. - - - - - - -The `{}` placeholder has a number of more advanced formatting options that we’ll discuss -later. - -After the `println` macro, we match the opening curly brace that declared the `main()` function with a closing curly brace to declare the end of the function: - -```rust,ignore -} -``` - -And of course, when we run the program, our output is: - -```text - -The value of x is: 5 - -``` - - - - -With this simple program, you've created your first variable and used your first Rust macro. That makes you a Rust programmer. Welcome! Now that you've seen the basics, let's explore variable bindings further. - -## Variable Bindings in Detail - - - -So far, we’ve created the simplest kind of variable binding, but the `let` statement has some tricks up its sleeve. -Let’s do some more complex things: create multiple bindings at once, how to add type annotations, mutating bindings, -shadowing, and more. - -### Creating Multiple Bindings - -The previous example program just bound one variable, but it's also possible to create multiple variable bindings in one go. Let’s try a more complex example, creating two variable bindings at once. Change your example program to this: - -```rust -fn main() { - let (x, y) = (5, 6); - - println!("The value of x is: {}", x); - println!("The value of y is: {}", y); -} -``` - -And enter `cargo run` to run it: - -```text -$ cargo run - Compiling bindings v0.1.0 (file:///projects/bindings) - Running `target/debug/bindings` -The value of x is: 5 -The value of y is: 6 -``` - - - -We’ve created two bindings with one `let` statement! - - - - - - - -The `let` statement binds the values in `(5, 6)` to the corresponding patterns of `(x, y)`. The first value `5` binds to the first part of the pattern, `x`, and the second value `6` binds to `y`. We could alternatively have used two `let` statements to the same effect, as follows: - - -```rust -fn main() { - let x = 5; - let y = 6; -} -``` - -In simple cases like this, where we are only binding two variables, two `let` statements may be clearer in the code, but when you're creating many multiple bindings, it's useful to be able to do so all at once. Deciding which technique to use is mostly a judgement call, and as you become more proficient in Rust, you’ll be able to figure out which style is better in each case. - -### Delayed Initialization - -The examples so far have all provided bindings with an initial value, but that isn't always necessary. Rather, we can assign a value for the binding later, after the `let` statement. To try this out, write the following program: - -```rust -fn main() { - let x; - - x = 5; - - println!("The value of x is: {}", x); -} -``` - -And enter `cargo run` to run it: - -```text -$ cargo run - Compiling bindings v0.1.0 (file:///projects/bindings) - Running `target/debug/bindings` -The value of x is: 5 -``` - -As you can see, this works just like the previous program, in which we assigned an initial value. - -This raises an interesting question: what happens if we try to print out a binding before we declare a value? Let's find out. Modify your code to look like the following: - - - - - -```rust,ignore -fn main() { - let x; - - println!("The value of x is: {}", x); - - x = 5; -} -``` - -When you enter `cargo run` to run this code, you should see output like this after the command: - -```text - Compiling bindings v0.1.0 (file:///projects/bindings) -src/main.rs:4:39: 4:40 error: use of possibly uninitialized variable: `x` [E0381] -src/main.rs:4 println!("The value of x is: {}", x); - ^ -:2:25: 2:56 note: in this expansion of format_args! -:3:1: 3:54 note: in this expansion of print! (defined in ) -src/main.rs:4:5: 4:42 note: in this expansion of println! (defined in ) -src/main.rs:4:39: 4:40 help: run `rustc --explain E0381` to see a detailed explanation -error: aborting due to previous error -Could not compile `bindings`. - -To learn more, run the command again with --verbose. -``` - - - -There's been an error! The compiler won’t let us write a program like this, and instead it requests that you declare a value. This is our first example of the compiler helping us find an error in our program. Different programming languages have different ways of approaching this problem. Some languages will always initialize values with some sort of default. Other languages leave the value uninitialized, and make no promises about what happens if you try to use something before initialization. Rust responds with an error to prod the programmer to declare the value they want. We must initialize any variable before we can use it. - - - - - -### Extended Error Explanations - -Now that you've seen an example of a Rust error, I want to point out one particularly useful aspect of errors. Rust encourages you to seek further information on the kind of error you've received with output like this: - -```text -src/main.rs:4:39: 4:40 help: run `rustc --explain E0381` to see a detailed explanation -``` - -This tells us that if we pass the `--explain` flag to `rustc` with the provided error code, we can see an extended explanation, which will try to explain common causes of and solutions to that kind of error. Not every error has a longer explanation, but many do. Here’s the explanation for the `E0381` error we received previously: - -```bash -$ rustc --explain E0381 -It is not allowed to use or capture an uninitialized variable. For example: - -fn main() { - let x: i32; - - let y = x; // error, use of possibly uninitialized variable - -To fix this, ensure that any declared variables are initialized before being -used. -``` - -These explanations can really help if you’re stuck on an error, so don't hesitate to look up the error code. The compiler is your friend, and it's there to help. - -### Mutable bindings - -By default, variable bindings are *immutable*, meaning that once a value is bound, you can't change that value. Try writing the following sample program to illustrate this: - -```rust,ignore -fn main() { - let x = 5; - - x = 6; - - println!("The value of x is: {}", x); -} -``` - -Save and run the program, and you should receive another error message, as in this output: - -```bash -$ cargo run - Compiling bindings v0.1.0 (file:///projects/bindings) -src/main.rs:4:5: 4:10 error: re-assignment of immutable variable `x` [E0384] -src/main.rs:4 x = 6; - ^~~~~ -src/main.rs:4:5: 4:10 help: run `rustc --explain E0384` to see a detailed explanation -src/main.rs:2:9: 2:10 note: prior assignment occurs here -src/main.rs:2 let x = 5; - ^ -``` - -The error includes the message `re-assigment of immutable variable` because the program tried to assign a second value to the `x` variable. But bindings are immutable only by default; you can make them mutable by adding `mut` in front of the variable name. For example, change the program you just wrote to the following: - -```rust -fn main() { - let mut x = 5; - - println!("The value of x is: {}", x); - - x = 6; - - println!("The value of x is: {}", x); -} -``` - -Running this, we get: - -```bash -$ cargo run - Compiling bindings v0.1.0 (file:///projects/bindings) - Running `target/debug/bindings` -The value of x is: 5 -The value of x is: 6 -``` - -Using `mut`, we change the value that `x` binds to from `5` to `6`. Note, however, that `mut` is part of the pattern in the `let` statement. This becomes more obvious if we add mutability to a pattern that binds multiple variables, like this: - -```rust,ignore -fn main() { - let (mut x, y) = (5, 6); - - x = 7; - y = 8; -} -``` - -If you run this code, the compiler will output an error: - -```bash -$ cargo build - Compiling bindings v0.1.0 (file:///projects/bindings) -src/main.rs:5:5: 5:10 error: re-assignment of immutable variable `y` [E0384] -src/main.rs:5 y = 8; - ^~~~~ -src/main.rs:5:5: 5:10 help: run `rustc --explain E0384` to see a detailed explanation -src/main.rs:2:17: 2:18 note: prior assignment occurs here -src/main.rs:2 let (mut x, y) = (5, 6); - ^ -``` - -The way `mut` is used here, the compiler is fine with reassigning the `x` variable, but not the `y` variable. That's because `mut` only applies to the name that directly follows it, not the whole pattern. For the compiler to allow you to reassign the `y` variable, you'd need to write the pattern as `(mut x, mut y)` instead. - - - - - - - -One thing to know about mutating bindings: `mut` allows you to mutate _the binding_, but not _what the name binds to_. In other words, the value is not what changes, but rather the path between the value and the name. For example: - - - - - -```rust -fn main() { - let mut x = 5; - - x = 6; -} -``` - -This does not change the value that `x` is bound to, but creates a new value (`6`) and changes the binding so that it binds the name `x` to this new value instead. This subtle but important difference will become more important as your Rust programs get more complex. - - - - - - -### Variable Binding Scope - - - - -Another important thing to know about variable bindings is that they are only valid as long as they are *in scope*. That scope begins at the point where the binding is declared, and ends at with the curley brace that closes the block of code containing it. We cannot access bindings "before they come into scope" or "after they go out of scope." Here’s an example to illustrate this: - - -```rust -fn main() { - println!("x is not yet in scope"); - - let x = 5; - - println!("x is now in scope"); - - println!("In real code, we’d now do a bunch of work."); - - println!("x will go out of scope now! The next curly brace is ending the main function."); -} -``` - -The variable binding for `x` goes out of scope with the last curly brace in the `main()` function. - - - - -This example only has one scope, though. In Rust, it's possible to create arbitrary scopes within a scope by placing code within another pair of curly braces. For example: - - - - -```rust - -fn main() { - - println!("x is not yet in scope"); - - let x = 5; - - println!("x is now in scope"); - - println!("Let’s start a new scope!"); - - { - let y = 5; - - println!("y is now in scope"); - println!("x is also still in scope"); - - println!("y will go out of scope now!"); - println!("The next curly brace is ending the scope we started."); - } - - println!("x is still in scope, but y is now out of scope and is not usable"); - - println!("x will go out of scope now! The next curly brace is ending the main function."); -} - -``` - - - - -The `y` variable is only in scope in the section of the code that's between the nested pair of curly braces, whereas `x` is in scope from the `let` statement that binds it until the final curly brace. The scope of bindings will become much more important later, as you learn about references in Chapter XX. - -### Shadowing Earlier Bindings - - - -One final thing about bindings: they can *shadow* previous bindings with the same name. Shadowing is what happens when you declare two bindings with the same name, we say that the second binding ‘shadows’ the first. - - - - -This can be useful if you’d like to perform a few transformations on a value, but still leave the binding immutable. For example: - -```rust -fn main() { - let x = 5; - - let x = x + 1; - - let x = x * 2; - - println!("The value of x is: {}", x); -} -``` - - - -This program first binds `x` to a value of `5`. Then, it shadows `x`, taking the original value and adding `1` so that the value of `x` is then `6`. The third `let` statement shadows `x` again, taking the previous value and multiplying it by `2` to give `x` a final value of `12`. If you run this, it will output: - - - - -```bash -$ cargo run - Compiling bindings v0.1.0 (file:///projects/bindings) - Running `target/debug/bindings` -The value of x is: 12 -``` - -Shadowing is useful because it lets us modify `x` without having to make the variable mutable. This means the compiler will still warn us if we accidentally try to mutate `x` directly later. For example, say after calculating `12` we don’t want `x` to be modified again; if we write the program in a mutable style, like this: - - - -```rust -fn main() { - let mut x = 5; - - x = x + 1; - x = x * 2; - - println!("The value of x is: {}", x); - - x = 15; - - println!("The value of x is: {}", x); -} -``` - -Rust is happy to let us mutate `x` again, to `15`. A similar program using the default immutable style, however, will let us know about that accidental mutation. Here's an example: - -```rust,ignore -fn main() { - let x = 5; - let x = x + 1; - let x = x * 2; - - println!("The value of x is: {}", x); - - x = 15; - - println!("The value of x is: {}", x); -} -``` - -If we try to compile this, we get an error: - -```bash -$ cargo build - Compiling bindings v0.1.0 (file:///projects/bindings) -src/main.rs:8:5: 8:11 error: re-assignment of immutable variable `x` [E0384] -src/main.rs:8 x = 15; - ^~~~~~ -src/main.rs:8:5: 8:11 help: run `rustc --explain E0384` to see a detailed explanation -src/main.rs:4:9: 4:10 note: prior assignment occurs here -src/main.rs:4 let x = x * 2; - ^ -error: aborting due to previous error -Could not compile `bindings`. -``` - -Since we don't want the binding to be mutable, this exactly what should happen. - -#### Shadowing Over Bindings - - - -You can also shadow bindings over one another, without re-using the initial binding. Here's how that looks: - -```rust - -fn main() { - let x = 5; - let x = 6; - - println!("The value of x is: {}", x); -} -``` - -Running this sample program, we can see the shadowing in action: - -```text -$ cargo run - Compiling bindings v0.1.0 (file:///projects/bindings) -src/main.rs:2:9: 2:10 warning: unused variable: `x`, #[warn(unused_variables)] on by default -src/main.rs:2 let x = 5; - ^ - Running `target/debug/bindings` -The value of x is: 6 - -``` - -Rust gives the value of `x` as `6`, which is the value from the *second* `let` statement. There are a few interesting things in this output. First, that Rust will compile and run the program without issue. This is because we haven't mutated the value; instead, we declared a _new_ binding that is _also_ named `x`, and gave it a new value. - - - - -The other interesting thing in this output is this error line: - -```text -src/main.rs:2:9: 2:10 warning: unused variable: `x`, #[warn(unused_variables)] on by default -``` - -Rust is pointing out that we shadowed `x`, but never used the initial value. Doing so isn’t _wrong_, but Rust is checking whether this is intentional and not just a mistake. In this case, the compiler issues a warning, but still compiles our program. A warning like this is called a *lint*, which is an old term for the bits of fluff and fibers in sheep’s wool that you wouldn't want to put in cloth. - -Similarly, this lint is telling us that we may have an extra bit of code (the statement `let x = 5`) that we don’t need. Even though our program works just fine, listening to these warnings and fixing the problems they point out is worthwhile, as they can be signs of a larger problem. In this case, we may not have realized that we were shadowing `x`, when we meant to, say, define a new variable with a different name. - -Shadowing can take some time to get used to, but it’s very powerful, and works well with immutability. - -#### Shadowing and Scopes - -Like any binding, a binding that shadows another binding becomes invalid at the end of a scope. Here’s an example program to illustrate this: - -```rust -fn main() { - let x = 5; - - println!("Before shadowing, x is: {}", x); - - { - let x = 6; - - println!("Now that x is shadowed, x is: {}", x); - } - - println!("After shadowing, x is: {}", x); -} -``` - -This code first creates the `x` variable and prints `x` to the terminal. Then, inside a new scope, it creates a new binding for `x` with a new value, and prints that value. When the arbitrary scope ends, `x` is printed once more. If we run this example, we can see the shadow appear and disappear in the output: - -```bash -$ cargo run - Compiling bindings v0.1.0 (file:///projects/bindings) - Running `target/debug/bindings` -Before shadowing, x is: 5 -Now that x is shadowed, x is: 6 -After shadowing, x is: 5 -``` - -In this case, the binding value reverts to the original value once the shadow binding goes out of scope. - - - -## How Functions Work in Rust - - - - -Functions are pervasive in Rust code. We’ve already seen one of the most important functions in the language: the `main()` function that’s the start of every program. We've also seen the `fn` keyword, which allows us to declare new functions. - - - - - - - -Rust code uses *snake case* as the conventional style for function names. In snake case, all letters are lower case, and there are underscores separating words. (Rust also uses snake case for the names of variable bindings; we just haven't used any variable bindings long enough to need underscores yet.) Here's a program containing an example function definition: - -```rust -fn main() { - println!("Hello, world!"); - - another_function(); -} - -fn another_function() { - println!("Another function."); -} -``` - - - - -Function definitions in Rust always start with `fn` and have a set of parentheses after the function name. The curly braces tell the compiler where the function begins and ends. - -We can call any function we’ve defined by entering its name followed by a pair of parentheses. Since `another_function()` is defined in the program, it can be called from inside the `main()` function. Note that we defined `another_function()` _after_ the `main()` function in our source code; we could have defined it before as well. Rust doesn’t care where you define your functions, only that they are defined somewhere. - -Let’s start a new project to explore functions further. Open a terminal, and navigate to the directory you're keeping your projects in. From there, use Cargo to generate a new project, as follows: - -```bash -$ cargo new --bin functions -$ cd functions -``` - - - - - - -Place the `another_function()` example in a file named *src/main.rs* and run it. You should see the following output: - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) - Running `target/debug/functions` -Hello, world! -Another function. -``` - -The lines execute in the order they appear in the `main()` function. First, our “Hello, world!” message prints, and then `another_function()` is called and its message is printed. - -### Function Arguments - - - - - -Functions can also take arguments. The following rewritten version of `another_function()` shows what arguments look like in Rust: - - -```rust -fn main() { - another_function(5); -} - -fn another_function(x: i32) { - println!("The value of x is: {}", x); -} -``` - - - - -Try running this program, and you should get this output: - - - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) - Running `target/debug/functions` -The value of x is: 5 -``` - -Since `main()` passed `5` to `another_function()`, the `println` macro put `5` where the pair of curly braces were in the format string. - -Let’s take a closer look at the signature of a function which takes a single argument: - - - -```text -fn NAME(PATTERN: TYPE) { -``` - -The parameter declaration in a single-argument function signature looks like the `let` bindings we used earlier. Just look at both together, and compare them: - -```rust,ignore -let x: i32; -fn another_function(x: i32) { -``` - - - - -The one difference is that in function signatures, we _must_ declare the type. This is a deliberate decision in the design of Rust; requiring type annotations in function definitions means you almost never need to use them elsewhere in the code. - - - - -When you want a function to have multiple arguments, just separate them inside the function signature with commas, like this: - -```text -fn NAME(PATTERN: TYPE, PATTERN: TYPE, PATTERN: TYPE, PATTERN: TYPE...) { -``` - -And just like a `let` declaration with multiple patterns, a type must be applied to each pattern separately. To demonstrate, here’s a full example of a function with multiple arguments: - -```rust -fn main() { - another_function(5, 6); -} - -fn another_function(x: i32, y: i32) { - println!("The value of x is: {}", x); - println!("The value of y is: {}", y); -} -``` - - - - -In this example, we make a function with two arguments. In this case, both are `i32`s, but if your function has multiple arguments, they don’t have to be the same time. They just happen to be in this example. Our function then prints out the values of both of its arguments. - -Let’s try out this code. Replace the program currently in your `function` project's `main.rs` file with the example above, and run it as follows: - - - -```bash - -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) - Running `target/debug/functions` -The value of x is: 5 -The value of y is: 6 -``` - -Since `5` is passed as the `x` argument and `6` is passed as the `y` argument, the two strings are printed with these values. - -### Bindings as Arguments - - - - -It's also possible to create bindings and pass them in as arguments in Rust. For example: - -```rust -fn main() { - let a = 5; - let b = 6; - - another_function(a, b); -} - -fn another_function(x: i32, y: i32) { - println!("The value of x is: {}", x); - println!("The value of y is: {}", y); -} -``` - -Instead of passing `5` and `6` directly, this first creates two bindings containing the values, and passes those bindings instead. When you run this, you'll find that it has the same effect as just using integers: - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) - Running `target/debug/functions` -The value of x is: 5 -The value of y is: 6 -``` - -Note that our bindings are called `a` and `b`, yet inside the function, we refer to them by the names in the signature, `x` and `y`. Inside a function, its parameters are in scope but the names of the bindings we passed as parameters are not, so we need to use the parameter names within the function block. Bindings passed as parameters don’t need to have the same names as the arguments. - -### Functions with Return Values - - - - -Functions can return values back to functions that call them. The signature for a function that returns a value looks like this: - -```TEXT -fn NAME(PATTERN: TYPE, PATTERN: TYPE, PATTERN: TYPE, PATTERN: TYPE...) -> TYPE { -``` - -In Rust, we don’t name return values, but we do declare their type, after the arrow (`->`). Here’s a sample program to illustrate this concept: - -```rust -fn main() { - let x = five(); - - println!("The value of x is: {}", x); -} - -fn five() -> i32 { - 5 -} -``` - -There are no function calls, macros, or even `let` statements in the `five()` function--just the number `5` by itself. That's a perfectly valid function in Rust. Note the function's return type, too. Try running this code, and the output should look like this: - - - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) - Running `target/debug/functions` -The value of x is: 5 -``` - - - - - -The `5` in `five()` is actually the function's return value, which is why the return type is `i32`. Let’s examine this in more detail. There are two important bits. First, the line `let x = five();` in `main()` shows that we can use the return value of a function to initialize a binding. - - - -Because the function `five()` returns a `5`, that line is the same as saying: - -```rust -let x = 5; -``` - -The second interesting bit is the `five()` function itself. It requires no arguments and defines the type of the return, but the body of the function is a lonely `5` with no semicolon. So far, we’ve ended almost every line in our programs with a semicolon, so why not here? - - - -The answer is that the return value of a function is the value of its final expression. To explain this, we have to go over statements and expressions. - -### Statements and Expressions - - - - -Expressions are bits of code that evaluate to a value. Consider a simple math operation, like this: - -```rust,ignore -5 + 6 -``` - -Evaluating this expression results in the value: `11`. In Rust, most bits of code are expressions. For example, calling a function is an expression: - - - -```rust,ignore -foo(5) -``` - -The value is equal to the return value of the `foo()` function. - -Statements are instructions. While expressions _compute_ something, statements perform some action. For example, `let` statements bind variables, and `fn` declarations are statements that begin functions. - -One practical difference between an expression and a statement is that you can bind an expression, but you can't bind a statement. - - - - -For example, `let` is a statement, so you can’t assign it to another binding, as this code tries to do: - -```rust,ignore -fn main() { - let x = (let y = 6); -} -``` - -If we were to run this program, we’d get an error like this: - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) -src/main.rs:2:14: 2:17 error: expected identifier, found keyword `let` -src/main.rs:2 let x = (let y = 6); - ^~~ -src/main.rs:2:18: 2:19 error: expected one of `!`, `)`, `,`, `.`, `::`, `{`, or an operator, found `y` -src/main.rs:2 let x = (let y = 6); - ^ -Could not compile `functions`. -``` - - - -In the same way, we can't assign a `fn` declaration to a binding, either. - -#### Expressions as Return Values - - - -So what does the way statements and expressions work have to do with return values? Well, the block that we use to create new scopes, `{}`, is an expression. Let’s take a closer look at `{}` with the following signature: - - - -```text -{ - STATEMENT* - EXPRESSION -} -``` - - - - -The `*` by `STATEMENT` indicates "zero or more," meaning we can have any number of statements inside a block, followed by an expression. Since blocks are expressions themselves, we can nest blocks inside of blocks. - - - - -And since blocks return a value, we can use them in `let` statements. For example: - -```rust -fn main() { - - let x = 5; - - let y = { - let z = 1; - - x + z + 5 - }; - - println!("The value of y is: {}", y); -} -``` - - - -Here, we're using a block to give us a value for the `y` variable. Inside that block, we create a new variable binding, `z`, with a `let` statement and give `z` a value. For the final expression of the block, we do some math, ultimately binding the result to `y`. Since `x` is `5` and `z` is `1`, the calculation is `5 + 1 + 5`, and so the value of the entire block is `11`. This gets substituted into our `let` statement for `y`, making that statement equivalent to: - -```rust,ignore -let y = 11; -``` - -Try running the program, and you should see the following output: - -```bash - Compiling functions v0.1.0 (file:///projects/functions) - Running `target/debug/functions` -The value of y is: 11 -``` - - - - - - - - - -As expected, the output string says that `y` is `11`. - -#### Functions Are Expressions (Or a different heading, if this one doesn't make sense.) - - -We also use blocks as the body of functions, for example: - - - - - -```rust -fn main() { - let x = 5; - - let y = { - x + 1 - }; - - println!("The value of y is: {}", y); - - let y = plus_one(x); - - println!("The value of y is: {}", y); -} - -fn plus_one(x: i32) -> i32 { - x + 1 -} -``` - -In both `let` statements that bind values to the `y` variable, we use a block to produce the value. In the first case, the block is an arbitrary scope nested within the `main()` function. In the second, the block is the body of the `plus_one()` function, which is passed `x` as a parameter. Running this gives: - - - - - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) - Running `target/debug/functions` -The value of y is: 6 -The value of y is: 6 -``` - -The `x` variable doesn't change before the new `y` variable is created and bound to the return value of the `plus_one()` function, so both `println` macros tell us that `y` is `6`. - - - - - - - - - - -#### Expression Statements - -Another impoortant thing to know about expressions and statements is that adding a semicolon to the end of an expression turns it into a statement. For example, look at this modified version of our `plus_one()` function from earlier: - -```rust,ignore -fn main() { - let x = plus_one(5); - - println!("The value of x is: {}", x); -} - -fn plus_one(x: i32) -> i32 { - x + 1; -} - -``` - - - - -Since `x + 1` is the only code in the function, it should be an expression, so that the value it evaluates to can be the function's return value. But the semicolon has turned it into a statement, so running this code would give an error, as follows: - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) -src/main.rs:7:1: 9:2 error: not all control paths return a value [E0269] -src/main.rs:7 fn plus_one(x: i32) -> i32 { -src/main.rs:8 x + 1; -src/main.rs:9 } -src/main.rs:7:1: 9:2 help: run `rustc --explain E0269` to see a detailed explanation -src/main.rs:8:10: 8:11 help: consider removing this semicolon: -src/main.rs:8 x + 1; - ^ -error: aborting due to previous error -Could not compile `functions`. -``` - -The main error message, "not all control paths return a value," reveals the core of the issue with this code. Statements don’t evaluate to a value, but `plus_one()` tries to return an `i32` with only a statement in the function body. In this output, Rust gives an option to rectify this: it suggests removing the semicolon, which would fix the error. - -In practice, Rust programmers don’t often think about these rules at this level. On a practical level, you should remember that you usually have a semicolon at the end of most lines, but ... - - -<-- thinking about this more, I did think of one: calling functions. foo() is an expression, and so foo() bar() is invalid. but adding ; to make them statements is okay, foo(); bar(); Maybe i can work up an example after all, what do you think? --> - - - - -### Returning Multiple Values - -By default, functions can only return single values. There’s a trick, however to get them to return multiple values. Remember how we used `()`s to create complex bindings in the "Creating Multiple Bindings" section on page XX? - -```rust -fn main() { - let (x, y) = (5, 6); -} -``` - -Braces used in this way form a *tuple*, which is a collection of elements that isn't assigned a name. Tuples are also a basic data type in Rust, and we'll cover them in detail in the "Tuples" section later in this chapter. For our purposes now, we can use tuples to return multiple values from functions, as so: - -```rust -fn main() { - let (x, y) = two_numbers(); - - println!("The value of x is: {}", x); - println!("The value of y is: {}", y); -} - -fn two_numbers() -> (i32, i32) { - (5, 6) -} -``` - - - - - -Running this will give us the values: - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) - Running `target/debug/functions` -The value of x is: 5 -The value of y is: 6 -``` - - - -Let's look at this more closely. First, we're assigning the return value of `two_numbers()` to `x` and `y`: - -```rust -fn two_numbers() -> (i32, i32) { - (5, 6) -} -``` - - - -In plain English, the `(i32, i32)` syntax translates to, “a tuple with two `i32`s in it." These two types are then applied to the tuple to be returned by the function block. In this case, that tuple contains the values `5` and `6`. This tuple is returned, and assigned to `x` and `y`: - -```rust,ignore -let (x, y) = two_numbers(); -``` - -See how all these bits fit together? We call this behavior of `let` ‘destructuring’, because it takes the structure of the expression that follows the `=` and takes it apart. - -## Data Types in Rust - -We’ve seen that every value in Rust is of a certain *type*, which tells Rust what kind of data is being given so it knows how to work with that data. As described in the "Type Inference and Annotation" section, you can rely on Rust's ability to infer types to figure out the type of a binding, or you can annotate it explicitly if needed. In this section, we'll look at a number of types built into the language itself. We'll look at two subsets of Rust data types: scalar and compound. - -### Type Inference and Annotation - - - - - -Rust is a *statically typed* language, which means that we must know the types of all bindings at compile time. However, you may have noticed that we didn’t declare a type for `x` or `y` in our previous examples. - - - -This is because Rust can often tell the type of a binding without you having to declare it. Annotating every single binding with a type can take uneccesary time and make code noisy. To avoid this, Rust uses *type inference*, meaning that it attempts to infer the types of your bindings from how the binding is used. Let’s look at the the first `let` statement you wrote again: - -```rust -fn main() { - let x = 5; -} -``` - - - -When we bind `x` to `5`, the compiler determines that `x` should be a numeric type based on the value it is bound to. Without any other information, it sets the `x` variable's type to `i32` (a thirty-two bit integer type) by default. We’ll talk more about Rust’s basic types in Section 3.3. - - - -If we were to declare the type with the variable binding, that would be called a *type annotation*. A `let` statement like that would look like this: - -```text - -let PATTERN: TYPE = VALUE; - -``` - - - -The `let` statement now has a colon after the `PATTERN`, followed by the `TYPE` name. Note that the colon and the `TYPE` go _after_ the `PATTERN`, not inside the pattern itself. Given this structure, here's how you'd rewrite `let x = 5` to use type annotation: - -```rust - -fn main() { - - let x: i32 = 5; - -} - -``` - -This does the same thing as `let x = 5` but explicitly states that `x` should be of the `i32` type. This is a simple case, but more complex patterns with multiple bindings can use type annotation, too. A binding with two variables would look like this: - - - -```rust - -fn main() { - - let (x, y): (i32, i32) = (5, 6); - -} - -``` - - - -In the same way as we place the `VALUE` and the `PATTERN` in corresponding positions, we also match up the position of the `TYPE` with the `PATTERN` it corresponds to. - - - - - - - - - -### Scalar Types - -A *scalar* type is one that represents a single value. There are four key scalar types in Rust: integers, floating point numbers, booleans, and characters. You'll likely recognize these from other programming languages, but let's jump into how they work in Rust. - - - -#### Integer Types - - - - -An *integer* is a number without a fractional component. We've used one integer type already in this chapter, the `i32` type. This type declaration indicates that the value it's associated with should be a signed integer (hence the `i`) for a 32-bit system. There are a number of built-in integer types in Rust, shown in Table 3-1. - -| Length | signed | unsigned | -|--------|--------|----------| -| 8-bit | i8 | u8 | -| 16-bit | i16 | u16 | -| 32-bit | i32 | u32 | -| 64-bit | i64 | u64 | -| arch | isize | usize | - -*Table 3-1: Integer types in Rust. The code, for example i32, is used to define a type in a function.* - - - -Each variant can be either signed or unsigned, and has an explicit size. Signed and unsigned merely refers to whether the number can be negative or positive. An unsigned number can only be positive, while a signed number can be either positive or negative. It's like writing numbers on paper: when the sign matters, a number is shown with a plus sign or minus sign, but when it's safe to assume the number is positive, it's shown with no sign. Signed numbers are stored using two’s complement representation. - - - - -Finally, the `isize` and `usize` types depend on the kind of computer your program is running on: 64-bits if you're on a 64-bit architecture, and 32-bits if you’re on a 32-bit architecture. - - - - - -So how do you know which type of integer to use? If you're unsure, Rust's defaults are generally good choices, and integer types default to `i32`: it’s generally the fastest, even on 64-bit systems. The primary situation in which you'd need to specify `isize` or `usize` is when indexing some sort of collection, which we'll talk about in the "Arrays" section. - - - - -#### Floating-Point Types - -Rust also has two primitive types for *floating-point numbers*, which are just numbers with decimal points, as usual. Rust's floating-point types are `f32` and `f64`, which are 32 bits and 64 bits in size, respectively. The default type is `f64`, as it’s roughly the same speed as `f32`, but has a larger precision. Here's an example showing floating-point numbers in action: - - - - -```rust -fn main() { - let x = 2.0; // f64 - - let y: f32 = 3.0; // f32 -} -``` - -Floating-point numbers are represented according to the IEEE-754 standard. The `f32` type is a single-precision float, while `f64` has double-precision. - - - - -#### Numeric Operations - -Rust supports the usual basic mathematic operations you’d expect for all of these number types--addition, subtraction, multiplication, division, and modulo. This code shows how you'd use each one in a `let` statement: - -```rust -fn main() { - // addition - let sum = 5 + 10; - - // subtraction - let difference = 95.5 - 4.3; - - // multiplication - let product = 4 * 30; - - // division - let quotient = 56.7 / 32.2; - - // modulus - let remainder = 43 % 5; -} - -``` - -Each expression in these statements uses a mathematical operator and evaluates to a single value, which is then bound to a variable. - - - - -#### The Boolean Type - -As in most other programming languages, a boolean type has two possible values: `true` and `false`. The boolean type in Rust is specified with `bool`. For example: - -```rust -fn main() { - let t = true; - - let f: bool = false; // with explict type annotation -} -``` - -The main way to consume boolean values is through conditionals like an `if` statement. We’ll cover how `if` statements work in Rust in the "Control Flow" section of this chapter. - -#### The Character Type - -So far we’ve only worked with numbers, but Rust supports letters too. Rust’s `char` type is the language's most primitive alphabetic type, and this code shows one way to use it: - -```rust -fn main() { - let c = 'z'; - - let z = 'ℤ'; -} -``` - -Rust’s `char` represents a Unicode Scalar Value, which means that it can represent a lot more than just ASCII. (You can learn more about Unicode Scalar Values at *http://www.unicode.org/glossary/#unicode_scalar_value*) A "character" isn’t really a concept in Unicode, however, so your human intutition for what a "character" is may not match up with what a `char` is in Rust. It also means that `char`s are four bytes each. - - - - - - - -### Compound Types - -*Compound types* can group multiple values of other types into another type. Rust has two primitive compound types: tuples and arrays. You can put a compound type inside a compound type as well. - - - - -#### Grouping Values into Tuples - -We’ve seen tuples already, when binding or returning multiple values at once. A tuple is a general way of grouping together some number of other values with distinct types into one compound type. The number of values is called the *arity* of the tuple. - -We create a tuple by writing a comma-separated list of values inside parentheses. Each position in the tuple has a distinct type, as in this example: - -```rust -fn main() { - let tup: (i32, f64, u8) = (500, 6.4, 1); -} -``` - -Note that, unlike the examples of multiple bindings, here we bind the single name `tup` to the entire tuple, emphasizing the fact that a tuple is considered a single compound element. We can then use pattern matching to destructure this tuple value, like this: - -```rust -fn main() { - let tup: (i32, f64, u8) = (500, 6.4, 1); - - let (x, y, z) = tup; - - println!("The value of y is: {}", y); -} -``` - -In this program, we first create a tuple, and bind it to the name `tup`. We then use a pattern with `let` to -take `tup` and turn it into three separate bindings, `x`, `y`, and `z`. This is called ‘destructuring’, because -it breaks the single tuple into three parts. - -Finally, we print the value of `x`, which is `6.4`. - -#### Tuple Indexing - -In addition to destructuring through pattern matching, we can also access a tuple element directly by using a period (`.`) followed by the index of the value we want to access. For example: - -```rust -fn main() { - let x: (i32, f64, u8) = (500, 6.4, 1); - - let five_hundred = x.0; - - let six_point_four = x.1; - - let one = x.2; -} -``` - - - - -This program creates a tuple, `x`, and then makes new bindings to each element by using their index. - -As with most programming languages, the first index in a tuple is 0. - - - -#### Single-Element Tuples - -Not everything contained within parentheses is a tuple in Rust. For example, a `(5)` may be a tuple, or just a `5` in parentheses. To disambiguate, use a comma for single-element tuples, as in this example: - -```rust -fn main() { - let x = (5); - - let x = (5,); -} - -``` - - - -In the first `let` statement, because `(5)` has no comma, it's a simple i32 and not a tuple. In the second `let` example, `(5,)` is a tuple with only one element. - -### Arrays - -So far, we’ve only represented single values in a binding. Sometimes, though, it’s useful to bind a name to more than one value. Data structures that contain multiple values are called *collections*, and arrays are the first type of Rust collection we’ll learn about. - - - - - -In Rust, arrays look like this: - -```rust -fn main() { - let a = [1, 2, 3, 4, 5]; -} -``` - -The values going into an array are written as a comma separated list inside square brackets. Unlike a tuple, -every element of an array must have the same type. - - - - -#### Type Annotation for Arrays - -When you specify an array’s type, you'd do so as such: - -```rust -fn main() { - let a: [i32; 5] = [1, 2, 3, 4, 5]; -} -``` - -Much like in a variable binding that uses type annotation, the array's type and length come after the pattern name and a colon. This array has `5` values, which are of the `i32` type. Unlike the values themselves, the type and array length are separated by a semicolon. - -####Accessing and Modifying Array Elements - -An array is a single chunk of memory, allocated on the stack. We can access elements of an array using indexing, like this: - -```rust -fn main() { - let a = [1, 2, 3, 4, 5]; - - let first = a[0]; - let second = a[1]; -} -``` - - - - -In this example, the `first` variable will bind to `1` at index `[0]` in the array, and `second` will bind to `2` at index `[1]` in the array. Note that these values are copied out of the array and into `first` and `second` when the `let` statement is called. That means if the array changes after the `let` statements, these bindings will not, and the two variables should retain their values. For example, imagine you have the following code: - - - -```rust -fn main() { - let mut a = [1, 2, 3, 4, 5]; - - let first = a[0]; - - a[0] = 7; - - println!("The value of first is: {}", first); -} -``` - - - - -First, notice the use of `mut` in the array declaration. We had to declare array `a` as `mut` to override Rust's default immutability. The line `a[0] = 7;` modifies the element at index 0 in the array, changing its value to `7`. This happens after `first` is bound to the original value at index 0, so `first` should still be equal `1`. Running the code will show this is true: - -```rust -The value of first is: 1 -``` - -Since `a[0]` didn't change until after `first` was assigned a value, the `println` macro replaced the `{}` with `1`, as expected. - - - - - -## Macros and Data Structures - -Now that we've discussed data structures a little, there are a couple of relevant macro concepts we should cover: the `panic!` macro and `Debug`, which is a new way of printing data to the terminal. - - - - -### Rectifying Invalid Indexes with `Panic!` - -Rust calls the `panic` macro when a program tries to access elements of an array (or any other data structure) and gives an invalid index. For an example, use the `functions` project we created on page XX and change your *src/main.rs* to look like this: - -```rust,should_panic -fn main() { - let a = [1, 2, 3, 4, 5]; - - let invalid = a[10]; - - println!("The value of invalid is: {}", invalid); -} -``` - -This program tries to access an element at index 10 in the `a` array. If we run it, we will get an error like this: - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) - Running `target/debug/functions` -thread ‘
’ panicked at ‘index out of bounds: the len is 5 but the index is 10’, src/main.rs:4 -Process didn’t exit successfully: `target/debug/functions` (exit code: 101) -``` - -The output tells us that our thread panicked, and that our program didn’t exit successfully. It also gives the reason: we requested an index of 10 from an array with a length of 5. - -So why did this cause Rust to panic? An array knows how many elements it holds. When we attempt to access an element using indexing, Rust will check that the index we've specified is less than the array length. If the index is greater than the length, it will panic. This is our first example of Rust’s safety principles in action. In many low-level languages, this kind of check is not done, and when you provide an incorrect index, invalid memory can be accessed. Rust protects us against this kind of error. We'll discuss more of Rust’s error handling in Chapter xx. - -### Using Debug in the println Macro - -So far, we’ve been printing values using `{}` in a `println` macro. If we try that with an array, however, we'll get an error. Say we have the following program: - -```rust,ignore -fn main() { - let a = [1, 2, 3, 4, 5]; - - println!("a is: {}", a); -} -``` - -This code tries to print the `a` array directly, which may seem innocuous. But running it produces the following output: - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) -src/main.rs:4:25: 4:26 error: the trait `core::fmt::Display` is not implemented for the type `[_; 5]` [E0277] -src/main.rs:4 println!(“a is {}”, a); - ^ -:2:25: 2:56 note: in this expansion of format_args! -:3:1: 3:54 note: in this expansion of print! (defined in ) -src/main.rs:4:5: 4:28 note: in this expansion of println! (defined in ) -src/main.rs:4:25: 4:26 help: run `rustc --explain E0277` to see a detailed explanation -src/main.rs:4:25: 4:26 note: `[_; 5]` cannot be formatted with the default formatter; try using `:?` instead if you are using a format string -src/main.rs:4:25: 4:26 note: required by `core::fmt::Display::fmt` -error: aborting due to previous error -``` - - - - -Whew! The core of the error is this part: the trait `core::fmt::Display` is not -implemented. We haven’t discussed traits yet, so this is bound to be confusing! -Here’s all we need to know for now: `println!` can do many kinds of formatting. -By default, `{}` implements a kind of formatting known as `Display`: output -intended for direct end-user consumption. The primitive types we’ve seen so far -implement `Display`, as there’s only one way you’d show a `1` to a user. But -with arrays, the output is less clear. Do you want commas or not? What about -the `[]`s? - -More complex types in the standard library do not automatically implement `Display` formatting. Instead, Rust implements another kind of formatting, also intended for the programmer. This formatting type is called `Debug`. To ask `println!` to use `Debug` formatting, we include `:?` in the print string, like this: - - - - -```rust -fn main() { - let a = [1, 2, 3, 4, 5]; - - println!("a is {:?}", a); -} -``` - -If you run this, it should print the five values in the `a` array as desired: - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) - Running `target/debug/functions` -a is [1, 2, 3, 4, 5] -``` - -You’ll see this repeated later, with other types. We’ll cover traits fully in Chapter 9. - -## Comments - -All programmers strive to make their code easy to understand, but sometimes some extra explanation is warranted. In these cases, we leave notes in our source code that the compiler will ignore. These notes are called *comments*. - -Here’s a simple comment: - -```rust -// Hello, world. -``` - -In Rust, comments must start with two slashes, and will last until the end of the line. For comments that extend beyond a single line, you'll need to include `//` on each line, like this: - -```rust -// So we’re doing something complicated here, long enough that we need -// multiple lines of comments to do it! Whew! Hopefully, this comment will -// explain what’s going on. -``` - -Comments can also be placed at the end of lines of code: - -```rust -fn main() { - let lucky_number = 7; // I’m feeling lucky today. -} -``` - -But you’ll more often see them above, like so: - -```rust -fn main() { - // I’m feeling lucky today. - let lucky_number = 7; -} -``` - -That’s all there is to it. Comments are not particularly complicated. - -### Documentation Comments - - - - -Rust has another kind of comment: a *documentation comment*. These comments don’t affect the way that the code works, but they do work with Rust’s tools. More specifically, the `rustdoc` tool can read documentation comments and produce HTML documentation from them. Documentation comments use an extra slash, like this: - -```rust -/// The foo function doesn’t really do much. -fn foo() { - -} - -/// Documentation comments can use -/// multiple line comments too, -/// like we did before. -fn bar() { - -} -``` - -The `rustdoc` tool would interpret each comment in this example as documenting the thing -that follows it. The first comment would be used to document the `foo()` function, and the second comment would document the `bar()` function. - -Because documentation comments have semantic meaning to `rustdoc`, the compiler will pay attention to the placement of your documentation comments. For example, a program containing only this: - -```rust,ignore -/// What am I documenting? -``` - -Will give the following compiler error: - -```text -src/main.rs:1:1: 1:27 error: expected item after doc comment -src/main.rs:1 /// What am I documenting? - ^~~~~~~~~~~~~~~~~~~~~~~~~~ -``` - -This happens because Rust expects a document comment to be associated with whatever code comes directly after it, so it sees that a document comment alone must be a mistake. - - - -## Control Flow with `if` - - - - -> Two roads diverged in a yellow wood, -> And sorry I could not travel both -> And be one traveler, long I stood -> And looked down one as far as I could -> To where it bent in the undergrowth; -> -> - Robert Frost, “The Road Not Taken” - - - - - - - - -In Rust, as in most programming languages, an `if` expression allows us to branch our code depending on conditions. We provide a condition, and then say, `if` this condition is met, then run this block of code; `if` the condition is not met, run a different block of code (or stop the program). - -Let’s make a new project to explore `if`. Navigate to your projects directory, -and use Cargo to make a new project called `branches`: - -```bash -$ cargo new --bin branches -$ cd branches -``` - -Write this sample program using `if` and save it in the *branches* directory: - -```rust -fn main() { - let condition = true; - - if condition { - println!("condition was true"); - } else { - println!("condition was false"); - } -} -``` - -The `condition` variable is a boolean; here, it's set to true. All `if` statements start with `if`, which is followed by a condition. The block of code we want to execute if the condition is true goes immediately after the condition, inside curly braces. These blocks are sometimes called ‘arms’. We can also include an `else` statement, which gives the program a block of code to execute should `condition` evaluate to false. - -Try running this code, and you should see output like this: - -```bash -$ cargo run - Compiling branches v0.1.0 (file:///projects/branches) - Running `target/debug/branches` -condition was true -``` - - - - -Before we talk about what’s happening here, let’s try changing the value of `condition` to `false` as follows: - -```rust - let condition = false; -``` - -Run the program again, and look at the output: - -```bash -$ cargo run - Compiling branches v0.1.0 (file:///projects/branches) - Running `target/debug/branches` -condition was false -``` - -This time the second block of code is run, the `else` block is executed. This is the very basic structure of `if`: _if_ the condition is true, then execute some code. If it’s not true, then execute some other code. When an `else` block is included, that "other code" is the code in the `else` block. You could also not use an `else` expression, as in this example: - -```rust -fn main() { - let condition = false; - - if condition { - println!("condition was true"); - } -} -``` - -In this case, nothing would be printed, because there is no code after the `if` block. - - - - -It’s also worth noting that `condition` here _must_ be a `bool`. To see what happens if the condition isn't a `bool`, try running this code: - - - - -```rust,ignore -fn main() { - let condition = 5; - - if condition { - println!("condition was five"); - } -} -``` - -The `condition` variable is assigned a value of `5` this time, and Rust will complain about it: - -```bash - Compiling branches v0.1.0 (file:///projects/branches) -src/main.rs:4:8: 4:17 error: mismatched types: - expected `bool`, - found `_` -(expected bool, - found integral variable) [E0308] -src/main.rs:4 if condition { - ^~~~~~~~~ -src/main.rs:4:8: 4:17 help: run `rustc --explain E0308` to see a detailed explanation -error: aborting due to previous error -Could not compile `branches`. -``` - -The error tells us that Rust expected a `bool`, but got an integer. Rust will not automatically try to convert non-boolean types to a boolean here. We must be explicit. - - - - -### Multiple Conditions with `else if` - -We can set multiple coniditions by combining `if` and `else` in an `else if` expression. For example: - -```rust -fn main() { - let number = 5; - - if number == 3 { - println!("condition was 3"); - } else if number == 4 { - println!("condition was 4"); - } else if number == 5 { - println!("condition was 5"); - } else { - println!("condition was something else"); - } -} -``` - -This program three possible paths it can take after checking the condition, and if you try running it, you should see output like this: - -```bash -$ cargo run - Compiling branches v0.1.0 (file:///projects/branches) - Running `target/debug/branches` -condition was 5 -``` - -When this program executes, it will check each `if` expression in turn, and execute the first body for which the condition holds true. - - - - -Using too many `else if` expressions can clutter your code, so if you find yourself with more than one, you may want to look at refactoring your code. In Chapter XX, we'll talk about a powerful Rust branching construct called `match` for these cases. - -### Using `if` in a Binding - -The last detail you need to learn about `if` is that it’s an expression. That means that we can use it on the right hand side of a `let` binding, for instance: - -```rust -fn main() { - let condition = true; - let number = if condition { - 5 - } else { - 6 - }; - - println!("The value of number is: {}", number); -} -``` - -The `number` variable will be bound to a value based on the outcome of the `if` expression. Let’s run this to see what happens: - -```bash -$ cargo run - Compiling branches v0.1.0 (file:///projects/branches) - Running `target/debug/branches` -The value of number is: 5 -``` - -Remember, blocks of code evaluate to the last expression in them, and numbers by themselves are also expressions. In this case, the value of the whole `if` expression depends on which block of code executes. This means that the value in both arms of the `if` must be the same type; in the previous example, they were both `i32` integers. But what happens if the types are mismatched, as in the following example? - -```rust,ignore -fn main() { - let condition = true; - - let number = if condition { - 5 - } else { - "six" - }; - - println!("The value of number is: {}", number); -} -``` - -The expression in one block of the `if` statement, is an integer and the expresion in the other block is a string. If we try to run this, we’ll get an error: - -```bash - Compiling branches v0.1.0 (file:///projects/branches) -src/main.rs:4:18: 8:6 error: if and else have incompatible types: - expected `_`, - found `&‘static str` -(expected integral variable, - found &-ptr) [E0308] -src/main.rs:4 let number = if condition { -src/main.rs:5 5 -src/main.rs:6 } else { -src/main.rs:7 "six" -src/main.rs:8 }; -src/main.rs:4:18: 8:6 help: run `rustc --explain E0308` to see a detailed explanation -error: aborting due to previous error -Could not compile `branches`. -``` - -The `if` and `else` arms have value types that are incompatible, and Rust tells us exactly where to find the problem in our program. This can’t work, because variable bindings must have a single type. - - - - -## Control Flow with Loops - -It’s often useful to be able to execute a block of code more than one time. For this, Rust has several constructs called *loops*. A loop runs through the code inside it to the end and then starts immediately back at the beginning. To try out loops, let’s make a new project. Navigate to your *projects* folder and use Cargo to make a new project: - -```bash -$ cargo new --bin loops -$ cd loops -``` - -There are three kinds of loops in Rust: `loop`, `while`, and `for`. Let’s dig -in. - -### Repeating Code with `loop` - - - -The `loop` keyword tells Rust to execute a block of code over and over again forever, or until we explicitly kill it. - -For an example, change the *src/main.rs* file in your *loops* directory to look like this: - -```rust,ignore -fn main() { - loop { - println!("again!"); - } -} -``` - -If we run this program, we’ll see `again!` printed over and over continuously until we kill the program manually. Most terminals support a keyboard shortcut, `control-c`, to kill a program stuck in a continual loop. Give it a try: - -```bash -$ cargo run - Compiling loops v0.1.0 (file:///projects/loops) - Running `target/debug/loops` -again! -again! -again! -again! -^Cagain! -``` -That `^C` there is where I hit `control-c`. Fortunately, Rust provides another, more reliable way to break out of a loop. We can place the `break` keyword within the loop to tell the program when to stop executing the loop. Try this version out of the program: - -```rust -fn main() { - loop { - println!("once!"); - break; - } -} -``` - -If you run this program, you’ll see that it only executes one -time: - -```bash -$ cargo run - Compiling loops v0.1.0 (file:///projects/loops) - Running `target/debug/loops` -once! -``` - - -When a Rust program hits a `break` statement, it will exit the current loop. This on its own is not very useful; if we wanted to print somtheing just once, we wouldn't put it in a loop. This is where conditions come in again. - - - - -### Conditional Loops With `while` - -To make `break` useful, we need to give our program a condition. While the condition is true, the loop runs. When the condition ceases to be true, the `break` code runs, stopping the loop. - - - - -Try this example: - -```rust - -fn main() { - let mut number = 3; - - loop { - if number != 0 { - println!("{}!", number); - - number = number - 1; - } else { - break; - } - } - - println!("LIFTOFF!!!"); -} -``` - -If we run this, we’ll get: - -```bash - Compiling loops v0.1.0 (file:///projects/loops) - Running `target/debug/loops` -3! -2! -1! -LIFTOFF!!! -``` - - - - -This program loops three times, counting down each time. Finally, after the -loop, it prints another message, then exits. - -The core of this example is in the combination of these three constructs: - -```rust,ignore - loop { - if number != 0 { - // do stuff - } else { - break; - } -``` - -We want to `loop`, but only while some sort of condition is true. As soon as it isn't, we want to `break` out of the loop. This pattern is so common that Rust has a more efficient language construct for it, called a `while` loop. Here's the same example, but using `while` instead: - - -```rust -fn main() { - let mut number = 3; - - while number != 0 { - println!("{}!", number); - - number = number - 1; - } - - println!("LIFTOFF!!!"); -} -``` - -This gets rid of a lot of nesting, and it's more clear. While a condition holds, -run this code. - -### Looping Though a Collection with `for` - -We can use this `while` construct to loop over the elements of a collection, like an array. For example: - -```rust -fn main() { - let a = [1, 2, 3, 4, 5]; - let mut index = 0; - - while index < 5 { - println!("the value is is: {}", a[index]); - - index = index + 1; - } -} -``` - -Here, we're counting up through the elements in the array. We start at index 0, then loop until we hit the final index of our array (that is, when `index < 5` is no longer true). Running this will print out every element of the array: - -```bash -$ cargo run - Compiling loops v0.1.0 (file:///projects/loops) - Running `target/debug/loops` -the value is: 1 -the value is: 2 -the value is: 3 -the value is: 4 -the value is: 5 -``` - - -All five array values appear in the terminal, as expected. Even though `index` will reach a value of `6` at some point, the loop stops executing before trying to fetch a sixth value from the array. - -This approach is error-prone, though; we could trigger a `panic!` by getting the index length incorrect. It's also slow, as the compiler needs to perform the conditional check on every element on every iteration through the loop. - -As a more efficient alternative, we can use a `for` loop. A `for` loop looks something like this: - -```rust -fn main() { - let a = [1, 2, 3, 4, 5]; - - let mut index = 0; - - for element in a.iter() { - println!("the value is: {}", element); - } -} - -``` - -** NOTE: see [https://github.com/rust-lang/rust/issues/25725#issuecomment-166365658](https://github.com/rust-lang/rust/issues/25725#issuecomment-166365658), we may want to change this ** - - -If we run this, we'll see the same output as the previous example. - - - -** I'm going to leave it at this for now until we decide how we want to do it** - diff --git a/nostarch/chapter5.md b/nostarch/chapter5.md deleted file mode 100644 index 17bfb7a22..000000000 --- a/nostarch/chapter5.md +++ /dev/null @@ -1,697 +0,0 @@ -# Structs - -So far, all of the data types we’ve seen allow us to have a single value -at a time. `struct`s give us the ability to package up multiple values and -keep them in one related structure. - -Let’s write a program which calculates the distance between two points. -We’ll start off with single variable bindings, and then refactor it to -use `struct`s instead. - -Let’s make a new project with Cargo: - -```bash -$ cargo new --bin points -$ cd points -``` - -Here’s a short program which calculates the distance between two points. Put -it into your `src/main.rs`: - -```rust -fn main() { - let x1 = 0.0; - let y1 = 5.0; - - let x2 = 12.0; - let y2 = 0.0; - - let answer = distance(x1, y1, x2, y2); - - println!("Point 1: ({}, {})", x1, y1); - println!("Point 2: ({}, {})", x2, y2); - println!("Distance: {}", answer); -} - -fn distance(x1: f64, y1: f64, x2: f64, y2: f64) -> f64 { - let x_squared = f64::powi(x2 - x1, 2); - let y_squared = f64::powi(y2 - y1, 2); - - f64::sqrt(x_squared + y_squared) -} -``` - -Let's try running this program with `cargo run`: - -```bash -$ cargo run - Compiling points v0.1.0 (file:///projects/points) - Running `target/debug/points` -Point 1: (0, 5) -Point 2: (12, 0) -Distance: 13 -``` - -Let's take a quick look at `distance()` before we move forward: - -```rust -fn distance(x1: f64, y1: f64, x2: f64, y2: f64) -> f64 { - let x_squared = f64::powi(x2 - x1, 2); - let y_squared = f64::powi(y2 - y1, 2); - - f64::sqrt(x_squared + y_squared) -} -``` - -To find the distance between two points, we can use the Pythagorean Theorem. -The theorem is named after Pythagoras, who was the first person to mathematically -prove this formula. The details aren't that important, to be honest. There's a few -things that we haven't discussed yet, though. - -```rust,ignore -f64::powi(2.0, 3) -``` - -The double colon (`::`) here is a namespace operator. We haven’t talked about -modules yet, but you can think of the `powi()` function as being scoped inside -of another name. In this case, the name is `f64`, the same as the type. The -`powi()` function takes two arguments: the first is a number, and the second is -the power that it raises that number to. In this case, the second number is an -integer, hence the ‘i’ in its name. Similarly, `sqrt()` is a function under the -`f64` module, which takes the square root of its argument. - -## Why `struct`s? - -Our little program is okay, but we can do better. The key is in the signature -of `distance()`: - -```rust,ignore -fn distance(x1: f64, y1: f64, x2: f64, y2: f64) -> f64 { -``` - -The distance function is supposed to calculate the distance between two points. -But our distance function calculates some distance between four numbers. The -first two and last two arguments are related, but that’s not expressed anywhere -in our program itself. We need a way to group `(x1, y1)` and `(x2, y2)` -together. - -We’ve already discussed one way to do that: tuples. Here’s a version of our program -which uses tuples: - -```rust -fn main() { - let p1 = (0.0, 5.0); - - let p2 = (12.0, 0.0); - - let answer = distance(p1, p2); - - println!("Point 1: {:?}", p1); - println!("Point 2: {:?}", p2); - println!("Distance: {}", answer); -} - -fn distance(p1: (f64, f64), p2: (f64, f64)) -> f64 { - let x_squared = f64::powi(p2.0 - p1.0, 2); - let y_squared = f64::powi(p2.1 - p1.1, 2); - - f64::sqrt(x_squared + y_squared) -} -``` - -This is a little better, for sure. Tuples let us add a little bit of structure. -We’re now passing two arguments, so that’s more clear. But it’s also worse. -Tuples don’t give names to their elements, and so our calculation has gotten -much more confusing: - -```rust,ignore -p2.0 - p1.0 -p2.1 - p1.1 -``` - -When writing this example, your authors almost got it wrong themselves! Distance -is all about `x` and `y` points, but now it’s all about `0` and `1`. This isn’t -great. - -Enter `struct`s. We can transform our tuples into something with a name: - -```rust,ignore -let p1 = (0.0, 5.0); - -struct Point { - x: f64, - y: f64, -} - -let p1 = Point { x: 0.0, y: 5.0 }; -``` - -Here’s what declaring a `struct` looks like: - -```text -struct NAME { - NAME: TYPE, -} -``` - -The `NAME: TYPE` bit is called a ‘field’, and we can have as many or as few of -them as you’d like. If you have none of them, drop the `{}`s: - -```rust -struct Foo; -``` - -`struct`s with no fields are called ‘unit structs’, and are used in certain -advanced situations. We will just ignore them for now. - -You can access the field of a struct in the same way you access an element of -a tuple, except you use its name: - -```rust,ignore -let p1 = (0.0, 5.0); -let x = p1.0; - -struct Point { - x: f64, - y: f64, -} - -let p1 = Point { x: 0.0, y: 5.0 }; -let x = p1.x; -``` - -Let’s convert our program to use our `Point` `struct`. Here’s what it looks -like now: - -```rust -#[derive(Debug,Copy,Clone)] -struct Point { - x: f64, - y: f64, -} - -fn main() { - let p1 = Point { x: 0.0, y: 5.0}; - - let p2 = Point { x: 12.0, y: 0.0}; - - let answer = distance(p1, p2); - - println!("Point 1: {:?}", p1); - println!("Point 2: {:?}", p2); - println!("Distance: {}", answer); -} - -fn distance(p1: Point, p2: Point) -> f64 { - let x_squared = f64::powi(p2.x - p1.x, 2); - let y_squared = f64::powi(p2.y - p1.y, 2); - - f64::sqrt(x_squared + y_squared) -} -``` - -Our function signature for `distance()` now says exactly what we mean: it -calculates the distance between two `Point`s. And rather than `0` and `1`, -we’ve got back our `x` and `y`. This is a win for clarity. - -There’s one other thing that’s a bit strange here, this annotation on our -`struct` declaration: - -```rust,ignore -#[derive(Debug,Copy,Clone)] -struct Point { -``` - -We haven’t yet talked about traits, but we did talk about `Debug` when we -discussed arrays. This `derive` attribute allows us to tweak the behavior of -our `Point`. In this case, we are opting into copy semantics, and everything -that implements `Copy` must implement `Clone`. -# Method Syntax - -In the last section on ownership, we made several references to ‘methods’. -Methods look like this: - -```rust -let s1 = String::from("hello"); - -// call a method on our String -let s2 = s1.clone(); - -println!("{}", s1); -``` - -The call to `clone()` is attatched to `s1` with a dot. This is called ‘method -syntax’, and it’s a way to call certain functions with a different style. - -Why have two ways to call functions? We’ll talk about some deeper reasons -related to ownership in a moment, but one big reason is that methods look nicer -when chained together: - -```rust,ignore -// with functions -h(g(f(x))); - -// with methods -x.f().g().h(); -``` - -The nested-functions version reads in reverse: we call `f()`, then `g()`, then -`h()`, but it reads as `h()`, then `g()`, then `f()`. - -Before we get into the details, let’s talk about how to define your own -methods. - -## Defining methods - -We can define methods with the `impl` keyword. `impl` is short for -‘implementation’. Doing so looks like this: - -```rust -#[derive(Debug,Copy,Clone)] -struct Point { - x: f64, - y: f64, -} - -impl Point { - fn distance(&self, other: &Point) -> f64 { - let x_squared = f64::powi(other.x - self.x, 2); - let y_squared = f64::powi(other.y - self.y, 2); - - f64::sqrt(x_squared + y_squared) - } -} - -let p1 = Point { x: 0.0, y: 0.0 }; -let p2 = Point { x: 5.0, y: 6.5 }; - -assert_eq!(8.200609733428363, p1.distance(&p2)); -``` - -Let’s break this down. First, we have our `Point` struct from earlier in the -chapter. Next comes our first use of the `impl` keyword: - -``` -# #[derive(Debug,Copy,Clone)] -# struct Point { -# x: f64, -# y: f64, -# } -# -impl Point { -# fn distance(&self, other: &Point) -> f64 { -# let x_squared = f64::powi(other.x - self.x, 2); -# let y_squared = f64::powi(other.y - self.y, 2); -# -# f64::sqrt(x_squared + y_squared) -# } -} -# -# let p1 = Point { x: 0.0, y: 0.0 }; -# let p2 = Point { x: 5.0, y: 6.5 }; -# -# assert_eq!(8.200609733428363, p1.distance(&p2)); -``` - -Everything we put inside of the curly braces will be methods implemented on -`Point`. - -``` -# #[derive(Debug,Copy,Clone)] -# struct Point { -# x: f64, -# y: f64, -# } -# -# impl Point { - fn distance(&self, other: &Point) -> f64 { -# let x_squared = f64::powi(other.x - self.x, 2); -# let y_squared = f64::powi(other.y - self.y, 2); -# -# f64::sqrt(x_squared + y_squared) - } -# } -# -# let p1 = Point { x: 0.0, y: 0.0 }; -# let p2 = Point { x: 5.0, y: 6.5 }; -# -# assert_eq!(8.200609733428363, p1.distance(&p2)); -``` - -Next is our definition. This looks very similar to our previous definition of -`distance()` as a function: - -```rust -# #[derive(Debug,Copy,Clone)] -# struct Point { -# x: f64, -# y: f64, -# } -fn distance(p1: Point, p2: Point) -> f64 { -# let x_squared = f64::powi(p2.x - p1.x, 2); -# let y_squared = f64::powi(p2.y - p1.y, 2); -# -# f64::sqrt(x_squared + y_squared) -# } -``` - -Other than this, the rest of the example is familliar: an implementation of -`distance()`, and using the method to find an answer. - -There are two differences. The first is in the first argument. Instead of a name -and a type, we have written `&self`. This is what distinguishes a method from a -function: using `self` inside of an `impl` block. Because we already know that -we are implementing this method on `Point`, we don’t need to write the type of -`self` out. However, we have written `&self`, not only `self`. This is because -we want to take our argument by reference rather than by ownership. In other -words, these two forms are the same: - -```rust,ignore -fn foo(self: &Point) -fn foo(&self) -``` - -Just like any other parameter, you can take `self` in three forms. Here’s the -list, with the most common form first: - -```rust,ignore -fn foo(&self) // take self by reference -fn foo(&mut self) // take self by mutable reference -fn foo(self) // take self by ownership -``` - -In this case, we only need a reference. We don’t plan on taking ownership, and -we don’t need to mutate either point. Taking by reference is by far the most -common form of method, followed by a mutable reference, and then occasionally -by ownership. - -### Methods and automatic referencing - -We’ve left out an important detail. It’s in this line of the example: - -``` -# #[derive(Debug,Copy,Clone)] -# struct Point { -# x: f64, -# y: f64, -# } -# -# impl Point { -# fn distance(&self, other: &Point) -> f64 { -# let x_squared = f64::powi(other.x - self.x, 2); -# let y_squared = f64::powi(other.y - self.y, 2); -# -# f64::sqrt(x_squared + y_squared) -# } -# } -# -# let p1 = Point { x: 0.0, y: 0.0 }; -# let p2 = Point { x: 5.0, y: 6.5 }; -# -assert_eq!(8.200609733428363, p1.distance(&p2)); -``` - -When we defined `distance()`, we took both `self` and the other argument by -reference. Yet, we needed a `&` for `p2` but not `p1`. What gives? - -This feature is called ‘automatic referencing’, and calling methods is one -of the few places in Rust that has behavior like this. Here’s how it works: -when you call a method with `self.(`, Rust will automatically add in `&`s -or `&mut`s to match the signature. In other words, these three are the same: - -```rust -# #[derive(Debug,Copy,Clone)] -# struct Point { -# x: f64, -# y: f64, -# } -# -# impl Point { -# fn distance(&self, other: &Point) -> f64 { -# let x_squared = f64::powi(other.x - self.x, 2); -# let y_squared = f64::powi(other.y - self.y, 2); -# -# f64::sqrt(x_squared + y_squared) -# } -# } -# let p1 = Point { x: 0.0, y: 0.0 }; -# let p2 = Point { x: 5.0, y: 6.5 }; -p1.distance(&p2); -(&p1).distance(&p2); -Point::distance(&p1, &p2); -``` - -The first one looks much, much cleaner. Here’s another example: - -```rust -let mut s = String::from("Hello,"); - -s.push_str(" world!"); - -// The above is the same as: -// (&mut s).push_str(" world!"); - -assert_eq!("Hello, world!", s); -``` - -Because [`push_str()`] has the following signature: - -```rust,ignore -fn push_str(&mut self, string: &str) { -``` - -[`push_str()`]: http://doc.rust-lang.org/collections/string/struct.String.html#method.push_str - -This automatic referencing behavior works because methods have a clear receiver -— the type of `self` — and in most cases it’s clear given the receiver and name -of a method whether the method is just reading (so needs `&self`), mutating (so -`&mut self`), or consuming (so `self`). The fact that Rust makes borrowing -implicit for method receivers is a big part of making ownership ergonomic in -practice. - -## Methods can be called like functions - -Furthermore, if we have a method, we can also call it like a function: - -```rust -# #[derive(Debug,Copy,Clone)] -# struct Point { -# x: f64, -# y: f64, -# } -# -# impl Point { -# fn distance(&self, other: &Point) -> f64 { -# let x_squared = f64::powi(other.x - self.x, 2); -# let y_squared = f64::powi(other.y - self.y, 2); -# -# f64::sqrt(x_squared + y_squared) -# } -# } -# let p1 = Point { x: 0.0, y: 0.0 }; -# let p2 = Point { x: 5.0, y: 6.5 }; -let d1 = p1.distance(&p2); -let d2 = Point::distance(&p1, &p2); - -assert_eq!(d1, d2); -``` - -Instead of using `self.(`, we use `Point` and the namespace operator to call it -like a function instead. Because functions do not do the automatic referencing, -we must pass in `&p1` explicitly. - -While methods can be called like functions, functions cannot be called like -methods. If the first argument isn’t named `self`, it cannot be called like a -method. -# Generics - -We've been working with a `Point` struct that looks like this: - -```rust -#[derive(Debug,Copy,Clone)] -struct Point { - x: f64, - y: f64, -} -``` - -But what if we didn't want to always use an `f64` here? What about an `f32` for -when we need less precision? Or an `i32` if we only want integer coordinates? - -While our simple `Point` struct may be a bit too simple to bother making -generic in a real application, we're going to stick with it to show you the -syntax. Especially when building library code, generics allow for more code -re-use, and unlock a lot of powerful techniques. - -## Generic data types - -'Generics' let us write code that allows for several different types, while -letting us have one definition. A more generic `Point` would look like this: - -```rust -#[derive(Debug,Copy,Clone)] -struct Point { - x: T, - y: T, -} -``` - -There are two changes here, and they both involve this new `T`. The first change -is in the definition: - -```rust -# #[derive(Debug,Copy,Clone)] -struct Point { -# x: T, -# y: T, -# } -``` - -Our previous definition said, "We are defining a struct named Point." This -definition says something slightly different: "We are defining a struct named -Point with one type parameter `T`." - -Let's talk about this term 'type parameter'. We've already seen one other thing -called a 'parameter' in Rust: function parameters: - -```rust -fn plus_one(x: i32) -> i32 { - x + 1 -} -``` - -Here, `x` is a parameter to this function. We can call this function with a -different value, and `x` will change each time it's called: - -```rust -# fn plus_one(x: i32) -> i32 { -# x + 1 -# } -let six = plus_one(5); -let eleven = plus_one(10); -``` - -In the same way, a type parameter allows us to define a data type which can be -different each time we use it: - -```rust -#[derive(Debug,Copy,Clone)] -struct Point { - x: T, - y: T, -} - -let integral_point = Point { x: 5, y: 5 }; -let floating_point = Point { x: 5.0, y: 5.0 }; -``` - -Here, `integral_point` uses `i32` values for `T`, and `floating_point` uses -`f64` values. This also leads us to talk about the second change we made to `Point`: - -```rust -# #[derive(Debug,Copy,Clone)] -# struct Point { - x: T, - y: T, -# } -``` - -Instead of saying `x: i32`, we say `x: T`. This `T` is the same one that we -used above in the struct declaration. Because `x` and `y` both use `T`, they'll -be the same type. We could give them different types: - -```rust -#[derive(Debug,Copy,Clone)] -struct Point { - x: T, - y: OtherT, -} - -let different = Point { x: 5, y: 5.0 }; -let same = Point { x: 5.0, y: 5.0 }; -``` - -Here, instead of a single parameter, `T`, we have two: `T` and `OtherT`. Type -parameters have the same naming convention as other types: `CamelCase`. -However, you'll often see short, one-letter names used for types. `T` is very -common, because it's short for 'type', but you can name them something longer -if you'd like. In this version of `Point`, we say that `x` has the type `T`, -and `y` has the type `OtherT`. This lets us give them two different types, but -they don't have to be. - -## Generic functions - -Regular old functions can also take generic parameters, with a syntax that looks -very similar: - -```rust -fn foo(x: T) { - // ... -} -``` - -This `foo()` function has one generic parameter, `T`, and takes one argument, -`x`, which has the type `T`. Let's talk a little bit more about what this means. - - -## Generic methods - -We've seen how to define methods with the `impl` keyword. Our generic `Point` -can have generic methods, too: - -```rust -#[derive(Debug,Copy,Clone)] -struct Point { - x: T, - y: T, -} - -impl Point { - fn some_method(&self) { - // ... - } -} -``` - -We also need the `` after `impl`. This line reads, "We will be implementing -methods with one generic type parameter, `T`, for a type, `Point`, which takes -one generic type `T`." In a sense, the `impl` says "we will be using a type -`T`" and the `Point` says "that `T` is used for `Point`." In this simple -case, this syntax can feel a bit redundant, but when we get into some of Rust's -more advanced features later, this distinction will become more useful. - -## There's more to the story - -This section covered the basic syntax of generics, but it's not the full story. -For example, let's try to implement our `foo()` function: we'll have it print out -the value of `x`: - -```rust,ignore -fn foo(x: T) { - println!("x is: {}", x); -} -``` - -We'll get an error: - -```text -error: the trait `core::fmt::Display` is not implemented for the type `T` [E0277] -println!("x is: {}", x); - ^ -``` - -We can't print out `x`! The error messages reference something we talked about -breifly before, the `Display` trait. In order to implement this function, we -need to talk about traits. But we only need to talk about traits to implement -our own generic functions; we don't need this understanding to use them. So -rather than get into more details about this right now, let's talk about other -useful Rust data types, and we can come back to implementing generic functions -in the chapter about traits. - -For now, the important bits to understand: - -* Generic type parameters are kind of like function parameters, but for types - instead of values. -* Type parameters go inside `<>`s and are usually named things like `T`. - -With that, let's talk about another fundamental Rust data type: enums. diff --git a/nostarch/chapter6.md b/nostarch/chapter6.md deleted file mode 100644 index a6c438e4a..000000000 --- a/nostarch/chapter6.md +++ /dev/null @@ -1,738 +0,0 @@ -# Enums - -Next, let’s look at a feature of Rust that’s similar to structs, but also -different. Enumerations, or ‘enums’ as they’re more commonly referred to, -are an extremely powerful feature of Rust. Enums are a feature that are in many -languages, but what they can do is different per-language. Rust’s enums are -most similar to enums in functional languages. - -Here’s an example of an enum: - -```rust -enum IpAddrKind { - V4, - V6, -} -``` - -This enum represents the kind of an IP address. There are two major standards -used for IP addresses: version four, and version six. Any IP address can be either -a version four address, or a version six address. But it cannot be both kinds at -the same time. This is where enums get their name: they allow us to enumerate all -of the possible kinds that our value can have. - -We can create values of `IpAddrKind` like this: - -```rust -# enum IpAddrKind { -# V4, -# V6, -# } - -let four = IpAddrKind::V4; -let six = IpAddrKind::V6; -``` - -Note that the variants of the enum are namespaced under its name, and we use -the double colon to separate the two. - -Enums have more tricks up their sleeves, however. Thinking more about our IP -address type, we don’t have a way to store the actual data of the IP address, -we only know what kind it is. Given that you just learned about structs, you -might tackle this problem like this: - -```rust -enum IpAddrKind { - V4, - V6, -} - -struct IpAddr { - kind: IpAddrKind, - address: String, -} - -let home = IpAddr { - kind: IpAddrKind::V4, - address: String::from("127.0.0.1"), -}; - -let loopback = IpAddr { - kind: IpAddrKind::V6, - address: String::from("::1"), -}; -``` - -We’ve used a struct to bundle the two values together: now we keep the kind -with the value itself. This design isn’t bad, exactly, but it wouldn’t be -considered idiomatic Rust. We can represent the same thing with just an enum: - -```rust -enum IpAddr { - V4(String), - V6(String), -} - -let home = IpAddr::V4(String::from("127.0.0.1")); - -let loopback = IpAddr::V6(String::from("::1")); -``` - -We can attach data to each variant of the enum directly. No need for an extra -struct. But beyond that, this approach is better than using a struct alongside -our enum because we can attatch different kinds of data to each variant. -Imagine that instead of a `String`, we would prefer to store a `V4` as its four -individual components, while leaving the `V6` variant as a `String`. With our -struct, we’d be stuck. But enums deal with this case with ease: - -```rust -enum IpAddr { - V4(u32, u32, u32, u32), - V6(String), -} - -let home = IpAddr::V4(127, 0, 0, 1); - -let loopback = IpAddr::V6(String::from("::1")); -``` - -You can put any kind of data inside of an enum variant, including another enum! -The `IpAddr` enum is [in the standard library][IpAddr], but it embeds two different -structs inside of its variants: - -```rust -struct Ipv4Addr { - // details elided -} - -struct Ipv6Addr { - // details elided -} - -enum IpAddr { - V4(Ipv4Addr), - V6(Ipv6Addr), -} -``` - -[IpAddr]: http://doc.rust-lang.org/std/net/enum.IpAddr.html - -Here’s an enum with a variety of types embedded in its variants: - -```rust -enum Message { - Quit, - Move { x: i32, y: i32 }, - Write(String), - ChangeColor(i32, i32, i32), -} -``` - -* `Quit` has no data associated with it at all. -* `Move` includes an anonymous struct inside of it. -* `Write` includes a single `String`. -* `ChangeColor` includes three `i32`s. - -We haven’t talked a lot about how to access the data inside an enum variant, -however. To do that, let’s move on to some new Rust syntax that’s especially -useful with enums: `match`. - - - - - - - - - - -# Option - -Now that we have a handle on enums, let's combine them with a feature that we -talked a little bit about in the previous chapter: generics. - -Programming language design is often though of as which features you include, -but it's also about which features you leave out. Rust does not have a feature -that is in many other languages: 'null'. In languages with this feature, -variables can have two states: null or not-null. - -The inventor of this concept has this to say: - -> I call it my billion-dollar mistake. At that time, I was designing the first -> comprehensive type system for references in an object-oriented language. My -> goal was to ensure that all use of references should be absolutely safe, with -> checking performed automatically by the compiler. But I couldn't resist the -> temptation to put in a null reference, simply because it was so easy to -> implement. This has led to innumerable errors, vulnerabilities, and system -> crashes, which have probably caused a billion dollars of pain and damage in -> the last forty years. -> -> - Tony Hoare "Null References: The Billion Dollar Mistake" - -The problem with null values is twofold: first, a value can be null or not, at -any time. The second is that if you try to use a value that's null, you'll get -an error of some kind, depending on the language. Because this property is -pervasive, it's extremely easy to make this kind of error. - -Even with these problems, the concept that null is trying to express is still a -useful one: this is a value which is currently invalid or not present for some -reason. The problem isn't with the concept itself, but with the particular -implementation. As such, Rust does not have the concept of null, but we do have -a type which can encode the concept of a value being present. We call this type -`Option`, and it looks like this: - -```rust -enum Option { - Some(T), - None, -} -``` - -This type is [provided by the standard library][option], and is so useful that -it's even in the prelude; you don't need to import it explicitly. Furthermore, -so are its variants: you can say `Some` and `None` directly, without prefixing -them with `Option::`. - -[option]: ../std/option/enum.Option.html - -Here's an example of using `Option`: - -```rust -let some_number = Some(5); -let some_string = Some("a string"); - -// If we only say None, we need to tell Rust what type of Option we have. -let absent_number: Option = None; -``` - -Let's dig in. First, you'll notice that we used the `` syntax when defining -`Option`: it's a generic enum. `Option` has two variants: `Some`, which -contains a `T`, and `None`, which has no data associated with it. In some -sense, `None` means 'null', and `Some` means 'not null'. So why is this any -better than null? - -In short, because `Option` and `T` are different types. That's a bit too -short though. Here's an example: - -```rust,ignore -let x = 5; -let y = Some(5); - -let sum = x + y; -``` - -This will not compile. We get an error message like this: - -```text -error: the trait `core::ops::Add>` is not implemented -for the type `_` [E0277] - -let sum = x + y; - ^~~~~ -``` - -Intense! What this error message is trying to say is that Rust does not -understand how to add an `Option` and a `T`. They're different types! This -shows one of the big advantages of an `Option` type: if you have a type that -may or may not exist, you have to deal with that fact before you can assume it -exists. In other words, you have to convert an `Option` to a `T` before you -can do `T` stuff with it. This helps catch one of the most common issues with -null, generally: assuming that something isn't null, when it actually is. - -So, how _do_ you get a `T` from an `Option`? The option type has a large -number of methods that you can check out in [its documentation], and becoming -familiar with them will be extremely useful in your journey with Rust. - -[its documentation]: ../std/option/enum.Option.html - -But we want a deeper understanding than that. If we didn't have those methods -defined for us already, what would we do? For that, we need a new feature: `match`. -# Match - -Rust has an extremely powerful control-flow operator: `match`. It allows us to -compare a value against a series of patterns, and then execute code based on -how they compare. Remember the `Option` type from the previous section? -Let's say that we want to write a function that takes an `Option`, and -if there's a value inside, add one to it. - -This function is very easy to write, thanks to `match`. It looks like this: - -```rust -fn plus_one(x: Option) -> Option { - match x { - None => None, - Some(i) => Some(i + 1), - } -} - -let five = Some(5); -let six = plus_one(five); -let none = plus_one(None); -``` - -Let's break down the `match`! At a high-level, the `match` expression looks -like this: - -```text -match condition { - pattern => code, -} -``` - -First, we have the `match` keyword. Next, we have a condition. This feels very -similar to an `if` expression, but there's a big difference: with `if`, the -condition needs to be a boolean. Here, it can be any type. - -Next, we have a "match arm". That's the part that looks like `pattern => -code,`. We can have as many arms as we need to: our `match` above has two -arms. An arm has two parts: a pattern, and some code. When the `match` -expression executes, it compares the condition against the pattern of each arm, -in turn. If the pattern matches the condition, the associated code is executed, -and the rest of the patterns are not checked. If it doesn't match, execution -continues to the next arm. - -Let's examine the first execution of `plus_one()` in more detail. In the above -example, `x` will be `Some(5)`. Let's compare that against each arm: - -```text -None => None, -``` - -Does `Some(5)` match `None`? No, it's the wrong variant. So let's continue. - -```text -Some(i) => Some(i + 1), -``` - -Does `Some(5)` match `Some(i)`? Why yes it does! We have the same variant. But -what about `i`? In a pattern like this, we can declare new bindings, similarly -to what we did with `let`. So in this case, the code part of the match arm will -have a binding, `i`, which corresponds to the `5`. - -With this arm, the code portion is `Some(i + 1)`. So we do exactly that: we -take `i`, which is `5`, add one to it, and create a new `Some` value with our -sum inside. - -Because `match` is an expression, the value of the overall expression becomes -the value of the arm that executed. So the value of this `match` expression -will be `Some(6)`. And since our `match` is the only expression in the -function, the value of the `match` will be the value of the function, and so -`Some(6)` is our return value as well, which is exactly what we were shooting -for. - -Now let's consider the second call. In this case, `x` is `None`. We enter the -`match`, and compare to the first arm: - -```text -None => None, -``` - -Does `None` match `None`? Yup! And so we return `None`. There's no value to add -to. - -Combining `match` and enums together is extremely powerful. You'll see this -pattern a lot in Rust code: `match` against an enum, binding to the data -inside, and then executing code based on it. It's a bit tricky at first, but -once you get used to it, you'll wish you had it in languages that don't support -it. It's consistently a user favorite. - -## Matches are exhaustive - -There's one other aspect of `match` we didn't talk about. Consider this version -of `plus_one()`: - -```rust,ignore -fn plus_one(x: Option) -> Option { - match x { - Some(i) => Some(i + 1), - } -} -``` - -A bug! We didn't handle the `None` case. Luckily, it's a bug Rust knows how to catch. -If we try to compile this code, we'll get an error: - -```text -error: non-exhaustive patterns: `None` not covered [E0004] -match x { - Some(i) => Some(i + 1), -} -``` - -Rust knows that we did not cover every possible option, and even knows which -pattern we forgot! This is referred to as being "exhaustive", we must exhaust -every last option possible in order to be valid! - -This analysis isn't perfect, however. This will also error: - -```rust,ignore -# let some_u8_value = 0u8; -match some_u8_value { - 0 => println!("zero"), - 1 => println!("one"), - 2 => println!("two"), - 3 => println!("three"), - 4 => println!("four"), - 5 => println!("five"), - 6 => println!("six"), - 7 => println!("seven"), - // We won't write out all of the arms here, but imagine that there are more - // arms corresponding to the rest of the numbers. - 254 => println!("two-hundred and fifty-four"), - 255 => println!("two-hundred and fifty-five"), -} -``` - -Even though a `u8` can only have valid values of zero through 255, Rust isn't -quite smart enough to understand we've covered all the cases. In order to fix -this, we can use a special pattern, `_`: - -```rust -# let some_u8_value = 0u8; -match some_u8_value { - 0 => println!("zero"), - 1 => println!("one"), - 2 => println!("two"), - 3 => println!("three"), - 4 => println!("four"), - 5 => println!("five"), - 6 => println!("six"), - 7 => println!("seven"), - // ... - 254 => println!("two-hundred and fifty-four"), - 255 => println!("two-hundred and fifty-five"), - _ => panic!("can't ever happen"), -} -``` - -The `_` pattern matches anything at all, and so with it as the final pattern, -Rust can understand that we have all our bases covered. It's not only used for -this sort of exhastiveness issue, though. It's useful any time we don't want to -deal with a number of cases. Consider this scenario: if we wanted to print out -something one one, three, five, and seven: - -```rust -# let some_u8_value = 0u8; -match some_u8_value { - 1 => println!("one"), - 3 => println!("three"), - 5 => println!("five"), - 7 => println!("seven"), - _ => (), -} -``` - -The `_` pattern will match all the other cases, and `()` will do nothing, it's -the unit value. - -## More about patterns - -As we've just seen, patterns are powerful, yet complex. Let's take a whole -section to cover all of the things that they can do. -# if let - -There's one more advanced control flow structure we haven't discussed: `if -let`. Imagine we're in a situation like this: - -```rust -# let some_option = Some(5); -match some_option { - Some(x) => { - // do something with x - }, - None => {}, -} -``` - -We care about the `Some` case, but don't want to do anything with the `None` -case. With an `Option`, this isn't _too_ bad, but with a more complex enum, -adding `_ => {}` after processing just one variant doesn't feel great. We have -this boilerplate arm, and we have an extra level of indentation: the code that -does something with `x` is indented twice, rather than just once. We really want -a construct that says "Do something with this one case, I don't care about the -others." - -Enter `if let`: - -```rust -# let some_option = Some(5); -if let Some(x) = some_option { - // do something with x -} -``` - -`if let` takes a pattern and an expression, separated by an `=`. It works -exactly like a `match`, where the expression is given to the `match` and the -pattern is its first arm. In other words, you can think of `if let` as syntax -sugar: - -```rust,ignore -if let pattern = expression { - body -} - -match expression { - pattern => body, - _ => {} -} -``` - -And in fact, we can include an `else` and it becomes the body of the `_` -case: - -```rust,ignore -if let pattern = expression { - body -} else { - else_body -} - -match expression { - pattern => body, - _ => else_body, -} -``` - -In other words, it's the high-level construct we were originally looking for: -do something with a single pattern. -# Patterns - -We've mentioned 'patterns' a few times so far: they're used in `let` bindings, -in function arguments, and in the `match` expression. Patterns have a lot of -abilities, so in this section, we'll cover all of the different things they can -do. Any of these abilities work in any place where a pattern is used. - -## Literals & _ - -You can match against literals directly, and `_` acts as an ‘any’ case: - -```rust -let x = 1; - -match x { - 1 => println!("one"), - 2 => println!("two"), - 3 => println!("three"), - _ => println!("anything"), -} -``` - -This prints `one`. - -# Multiple patterns - -You can match multiple patterns with `|`: - -```rust -let x = 1; - -match x { - 1 | 2 => println!("one or two"), - 3 => println!("three"), - _ => println!("anything"), -} -``` - -This prints `one or two`. - -## ref and ref mut - -Usually, when you match against a pattern, bindings are bound by value. -This means you'll end up moving the value out: - -```rust,ignore -let name = Some(String::from("Bors")); - -match name { - Some(name) => println!("Found a name: {}", name), - None => (), -} - -// name is moved here. This line will fail to compile: -println!("name is: {:?}", name); -``` - -If you'd prefer to bind `name` by reference, use the `ref` keyword: - -```rust -let name = Some(String::from("Bors")); - -match name { - Some(ref name) => println!("Found a name: {}", name), - None => (), -} - -// name is not moved here; the match only took a reference to its data rather -// than moving it. This will work: -println!("name is: {:?}", name); -``` - -And for a mutable reference, `ref mut`: - -```rust -let mut name = Some(String::from("Bors")); - -match name { - Some(ref mut name) => *name = String::from("Another name"), - None => (), -} - -// name is not moved here; the match only took a reference to its data rather -// than moving it -``` - -## Destructuring - -Patterns can be used to destructure structs and enums: - -```rust -struct Point { - x: i32, - y: i32, -} - -let origin = Point { x: 0, y: 0 }; - -let Point { x, y } = origin; -``` - -This brings an `x` and `y` binding into scope, matching the `x` and `y` of -`origin`. While it can be unusual in `let`, this is the same principle of -patterns in `match`: - -```rust -struct Point { - x: i32, - y: i32, -} - -let origin = Point { x: 0, y: 0 }; - -match origin { - Point { x, y } => { }, // x and y are bound here -} -``` - -## Shadowing - -As with all bindings, anything bound by a pattern will shadow bindings -outside of the binding construct: - -```rust -let x = Some(5); - -match x { - Some(x) => { }, // x is an i32 here, not an Option - None => (), -} -``` - -## Ignoring bindings - -We discussed using `_` as a whole pattern to ignore it above, but you can -also use `_` inside of another pattern to ignore just part of it: - -```rust -let x = Some(5); - -match x { - Some(_) => println!("got a Some and I don't care what's inside"), - None => (), -} -``` - -Or like this: - -```rust -let numbers = (2, 4, 8, 16, 32); - -match numbers { - (first, _, third, _, fifth) => println!("Some numbers: {}, {}, {}", first, third, fifth), -} -``` - -If you want, you can use `..` to ignore all of the parts you haven't defined: - -```rust -struct Point { - x: i32, - y: i32, - z: i32, -} - -let origin = Point { x: 0, y: 0, z: 0 }; - -match origin { - Point { x, .. } => { }, // y and z are ignored -} -``` - -## Ranges - -You can match a range of values with `...`: - -```rust -let x = 5; - -match x { - 1 ... 5 => println!("one through five"), - _ => println!("something else"), -} -``` - -Ranges are usually used with integers or `char`s: - -```rust -fn main() { - let x = 'c'; - - match x { - 'a' ... 'j' => println!("early ASCII letter"), - 'k' ... 'z' => println!("late ASCII letter"), - _ => println!("something else"), - } -} -``` - -## Guards - -You can introduce ‘match guards’ with `if`: - -```rust -let x = Some(5); - -match x { - Some(x) if x < 5 => println!("less than five: {}", x), - Some(x) => println!("{}", x), - None => (), -} -``` - -If you’re using if with multiple patterns, the if applies to both sides: - -```rust -let x = 4; -let y = false; - -match x { - 4 | 5 if y => println!("yes"), - _ => println!("no"), -} -``` - -This prints `no`, because the if applies to the whole of `4 | 5`, and not to only -the `5`. In other words, the precedence of if behaves like this: - -```text -(4 | 5) if y => ... -``` - -not this: - -```text -4 | (5 if y) => ... -``` - -## Bindings - -You can bind values to names with `@`: diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 9fdd6cb8e..88ec05e8a 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -1,34 +1,30 @@ # The Rust Programming Language -- [Introduction](ch01-01-introduction.md) - - [Installation](ch01-02-installation.md) - - [Hello, World!](ch01-03-hello-world.md) - - [The Design of Rust](ch01-04-design.md) +- [Introduction](ch01-00-introduction.md) + - [Installation](ch01-01-installation.md) + - [Hello, World!](ch01-02-hello-world.md) -- [Tutorial]() +- [Guessing Game Tutorial](ch02-00-guessing-game-tutorial.md) -- [Up and Running](ch03-01-up-and-running.md) - - [Variable Bindings](ch03-02-variable-bindings.md) - - [Functions](ch03-03-functions.md) - - [Scalar Types](ch03-04-scalar-types.md) - - [Compound Types](ch03-05-compound-types.md) - - [Comments](ch03-06-comments.md) - - [Control flow with `if`](ch03-07-if.md) - - [Loops](ch03-08-loops.md) +- [Common Programming Concepts in Rust](ch03-00-common-programming-concepts-in-rust.md) + - [Variable Bindings and Mutability](ch03-01-variable-bindings-and-mutability.md) + - [Data Types](ch03-02-data-types.md) + - [How Functions Work](ch03-03-how-functions-work.md) + - [Comments](ch03-04-comments.md) + - [Control Flow](ch03-05-control-flow.md) -- [Understanding Ownership](ch04-01-understanding-ownership.md) - - [Ownership](ch04-02-ownership.md) - - [References & Borrowing](ch04-03-references-and-borrowing.md) - - [Slices](ch04-04-slices.md) +- [Understanding Ownership](ch04-00-understanding-ownership.md) + - [Ownership](ch04-01-ownership.md) + - [References & Borrowing](ch04-02-references-and-borrowing.md) + - [Slices](ch04-03-slices.md) -- [Structs](ch05-01-structs.md) - - [Method Syntax](ch05-02-method-syntax.md) - - [Advanced]() +- [Structs](ch05-00-structs.md) + - [Method Syntax](ch05-01-method-syntax.md) -- [Enums](ch06-01-enums.md) - - [Option](ch06-02-option.md) - - [Match](ch06-03-match.md) - - [if let](ch06-04-if-let.md) +- [Enums](ch06-00-enums.md) + - [Option](ch06-01-option.md) + - [Match](ch06-02-match.md) + - [if let](ch06-03-if-let.md) - [Crates & Modules]() @@ -100,3 +96,6 @@ - [Macros]() - [Nightly Rust]() + +- [Appendix](appendix-00.md) + - [Keywords](appendix-01-keywords.md) diff --git a/src/appendix-00.md b/src/appendix-00.md new file mode 100644 index 000000000..83a7e9132 --- /dev/null +++ b/src/appendix-00.md @@ -0,0 +1,4 @@ +# Appendix + +The following sections contain reference material you may find useful in your +Rust journey. diff --git a/src/appendix-01-keywords.md b/src/appendix-01-keywords.md new file mode 100644 index 000000000..1957f95c9 --- /dev/null +++ b/src/appendix-01-keywords.md @@ -0,0 +1,58 @@ +## Keywords + +The following keywords are reserved by the Rust language and may not be used as +names of functions, variables, macros, modules, crates, constants, static +values, attributes, struct fields, or arguments. + +* `abstract` +* `alignof` +* `as` +* `become` +* `box` +* `break` +* `const` +* `continue` +* `crate` +* `do` +* `else` +* `enum` +* `extern` +* `false` +* `final` +* `fn` +* `for` +* `if` +* `impl` +* `in` +* `let` +* `loop` +* `macro` +* `match` +* `mod` +* `move` +* `mut` +* `offsetof` +* `override` +* `priv` +* `proc` +* `pub` +* `pure` +* `ref` +* `return` +* `Self` +* `self` +* `sizeof` +* `static` +* `struct` +* `super` +* `trait` +* `true` +* `type` +* `typeof` +* `unsafe` +* `unsized` +* `use` +* `virtual` +* `where` +* `while` +* `yield` diff --git a/src/ch01-00-introduction.md b/src/ch01-00-introduction.md new file mode 100644 index 000000000..badc11291 --- /dev/null +++ b/src/ch01-00-introduction.md @@ -0,0 +1,31 @@ +# Introduction + +Welcome to “The Rust Programming Language”, an introductory book about Rust. +Rust is a programming language that’s focused on safety, speed, and +concurrency. Its design lets you create programs that have the performance and +control of a low-level language, but with helpful abstractions that feel like a +high-level language. The Rust community welcomes all programmers who have their +experience in languages like C and are looking for a safer alternative, as well +as programmers from languages like Python who are looking for ways to write more +performant code without losing expressiveness. + +Rust provides the majority of its safety checks at compile time and without a +garbage collector so that your program's runtime isn't impacted. This makes it +useful in a number of use cases that other languages aren’t good at: embedding +in other languages, programs with specific space and time requirements, and +writing low-level code, like device drivers and operating systems. It's also +great for web applications: it powers the Rust package registry site, crates.io! +We're excited to see what _you_ create with Rust. + +This book is written for a reader who already knows how to program in at least +one programming language. After reading this book, you should be comfortable +writing Rust programs. We’ll be learning Rust through small, focused examples +that build on each other to demonstrate how to use various features of Rust as +well as how they work behind the scenes. + +## Contributing to the book + +This book is open source. If you find an error, please don’t hesitate to file an +issue or send a pull request [on GitHub]. + +[on GitHub]: https://github.com/rust-lang/book diff --git a/src/ch01-02-installation.md b/src/ch01-01-installation.md similarity index 51% rename from src/ch01-02-installation.md rename to src/ch01-01-installation.md index cba8d82ca..07652030b 100644 --- a/src/ch01-02-installation.md +++ b/src/ch01-01-installation.md @@ -1,50 +1,51 @@ -# Installation +## Installation -The first step to using Rust is to install it. Generally speaking, you’ll need -an internet connection to run the commands in this chapter, as we’ll be -downloading Rust from the internet. +The first step to using Rust is to install it. You’ll need an internet +connection to run the commands in this chapter, as we’ll be downloading Rust +from the internet. We’ll be showing off a number of commands using a terminal, and those lines all -start with `$`. You don't need to type in the `$`s, they are there to indicate +start with `$`. You don't need to type in the `$`s; they are there to indicate the start of each command. You’ll see many tutorials and examples around the web that follow this convention: `$` for commands run as a regular user, and `#` -for commands you should be running as an administrator. +for commands you should be running as an administrator. Lines that don't start +with `$` are typically showing the output of the previous command. -## Installing on Linux or Mac +### Installing on Linux or Mac -If you're on Linux or a Mac, all you need to do is open a terminal and type this: +If you're on Linux or a Mac, all you need to do is open a terminal and type +this: ```bash -$ curl -sSf https://static.rust-lang.org/rustup.sh | sh +$ curl https://sh.rustup.rs -sSf | sh ``` -This will download a script, and start the installation. You may be prompted for your password. -If it all goes well, you’ll see this appear: +This will download a script and start the installation. You may be prompted for +your password. If it all goes well, you’ll see this appear: -```text - Rust is ready to roll. +```bash +Rust is installed now. Great! ``` - -## Installing on Windows +### Installing on Windows If you're on Windows, please download the appropriate [installer][install-page]. [install-page]: https://www.rust-lang.org/install.html -## Uninstalling +### Uninstalling Uninstalling Rust is as easy as installing it. On Linux or Mac, just run the uninstall script: ```bash -$ sudo /usr/local/lib/rustlib/uninstall.sh +$ rustup self uninstall ``` -If you used the Windows installer, you can re-run the `.msi` and it will give you -an uninstall option. +If you used the Windows installer, you can re-run the `.msi` and it will give +you an uninstall option. -## Troubleshooting +### Troubleshooting If you've got Rust installed, you can open up a shell, and type this: @@ -52,9 +53,15 @@ If you've got Rust installed, you can open up a shell, and type this: $ rustc --version ``` -You should see the version number, commit hash, and commit date. +You should see the version number, commit hash, and commit date in a format +similar to this for the latest stable version at the time you install: -If you do, Rust has been installed successfully! Congrats! +```bash +rustc x.y.z (abcabcabc yyyy-mm-dd) +``` + +If you see this, Rust has been installed successfully! +Congrats! If you don't and you're on Windows, check that Rust is in your `%PATH%` system variable. If it isn't, run the installer again, select "Change" on the "Change, @@ -64,16 +71,16 @@ If not, there are a number of places where you can get help. The easiest is [the #rust IRC channel on irc.mozilla.org][irc], which you can access through [Mibbit][mibbit]. Click that link, and you'll be chatting with other Rustaceans (a silly nickname we call ourselves) who can help you out. Other great resources -include [the user’s forum][users], and [Stack Overflow][stackoverflow]. +include [the user’s forum][users] and [Stack Overflow][stackoverflow]. [irc]: irc://irc.mozilla.org/#rust [mibbit]: http://chat.mibbit.com/?server=irc.mozilla.org&channel=%23rust [users]: https://users.rust-lang.org/ [stackoverflow]: http://stackoverflow.com/questions/tagged/rust -## Local documentation +### Local documentation -This installer also installs a copy of the documentation locally, so you can -read it offline. On UNIX systems, `/usr/local/share/doc/rust` is the location. -On Windows, it's in a `share/doc` directory, inside the directory to which Rust -was installed. +The installer also includes a copy of the documentation locally, so you can +read it offline. On Linux or Mac, run `rustup doc` to open the local +documentation in your browser. On Windows, the documentation is in a +`share/doc` directory inside the directory where Rust was installed. diff --git a/src/ch01-01-introduction.md b/src/ch01-01-introduction.md deleted file mode 100644 index 51f618b3c..000000000 --- a/src/ch01-01-introduction.md +++ /dev/null @@ -1,31 +0,0 @@ -# Introduction - -Welcome to “The Rust Programming Language”, an introductory book about Rust. -Rust is a programming language that’s focused on safety, concurrency, and -speed. It maintains these goals without having a garbage collector, making it a -useful language for a number of use cases other languages aren’t good at: -embedding in other languages, programs with specific space and time -requirements, and writing low-level code, like device drivers and operating -systems. It improves on current languages targeting this space by having a -number of compile-time safety checks that produce no runtime overhead, while -eliminating all data races. Rust also aims to achieve ‘zero-cost abstractions’ -even though some of these abstractions feel like those of a high-level -language. Even then, Rust still allows precise control like a low-level -language would. - -This book is written for a reader who already knows how to program in at least -one programming language. Which language that is does not matter very much, -though you may have an easier time if you’ve programmed in a low-level language -with manual memory allocation. - -After reading this book, you should be comfortable writing Rust programs. We’ll -be learning Rust through small, focused examples that demonstrate each topic. -The chapters build upon each other, so if you skip ahead, you may have to skip -back to refer to a previous concept. - -## Contributing to the book - -This book is open source. If you find an error, please don’t hesitate to file an -issue or send a pull request [on GitHub]. - -[on GitHub]: https://github.com/rust-lang/book diff --git a/src/ch01-02-hello-world.md b/src/ch01-02-hello-world.md new file mode 100644 index 000000000..f7d1bf439 --- /dev/null +++ b/src/ch01-02-hello-world.md @@ -0,0 +1,373 @@ +## Hello, World! + +Now that you have Rust installed, let’s write your first Rust program. It's +traditional when learning a new language to write a little program to print the +text “Hello, world!” to the screen, and in this section, we'll follow that +tradition. + +> Note: This book assumes basic familiarity with the command line. Rust itself +> makes no specific demands about your editing, tooling, or where your code +> lives, so if you prefer an IDE to the command line, that's an option. + +### Creating a Project File + +First, make a file to put your Rust code in. Rust doesn't care where your code +lives, but for this book, we'd suggest making a *projects* directory in your +home directory and keeping all your projects there. Open a terminal and enter +the following commands to make a directory for this particular project: + +```bash +$ mkdir ~/projects +$ cd ~/projects +$ mkdir hello_world +$ cd hello_world +``` + +> Note: If you’re on Windows and not using PowerShell, the `~` that represents +> your home directory may not work. +> Consult the documentation for your shell for more details. + +### Writing and Running a Rust Program + +Next, make a new source file and call it *main.rs*. Rust files always end with +the *.rs* extension. If you’re using more than one word in your filename, use +an underscore to separate them. For example, you'd use *hello_world.rs* rather +than *helloworld.rs*. + +Now open the *main.rs* file you just created, and type the following code: + +```rust +fn main() { + println!("Hello, world!"); +} +``` + +Save the file, and go back to your terminal window. On Linux or OSX, enter the +following commands: + +```bash +$ rustc main.rs +$ ./main +Hello, world! +``` + +On Windows, just replace `main` with `main.exe`. Regardless of your operating +system, you should see the string `Hello, world!` print to the terminal. If you +did, then congratulations! You've officially written a Rust program. That makes +you a Rust programmer! Welcome. + +### Anatomy of a Rust Program + +Now, let’s go over what just happened in your "Hello, world!" program in +detail. Here's the first piece of the puzzle: + +```rust +fn main() { + +} +``` + +These lines define a *function* in Rust. The `main` function is special: it's +the first thing that is run for every executable Rust program. The first line +says, “I’m declaring a function named `main` that takes no arguments and +returns nothing.” If there were arguments, they would go inside the parentheses +(`(` and `)`). We aren’t returning anything from this function, so we have +omitted the return type entirely. If there was a return type, there would be a +`->` and the return type after the parentheses. + +Also note that the function body is wrapped in curly braces (`{` and `}`). Rust +requires these around all function bodies. It's considered good style to put +the opening curly brace on the same line as the function declaration, with one +space in between. + +Inside the `main()` function: + +```rust + println!("Hello, world!"); +``` + +This line does all of the work in this little program: it prints text to the +screen. There are a number of details that are important here. The first is +that it’s indented with four spaces, not tabs. + +The second important part is `println!()`. This is calling a Rust *macro*, +which is how metaprogramming is done in Rust. If it were calling a function +instead, it would look like this: `println()` (without the `!`). We'll discuss +Rust macros in more detail in Chapter XX, but for now you just need to know +that when you see a `!` that means that you’re calling a macro instead of a +normal function. + +Next is `"Hello, world!"` which is a *string*. We pass this string as an +argument to `println!()`, which prints the string to the screen. Easy enough! + +The line ends with a semicolon (`;`). The `;` indicates that this expression is +over, and the next one is ready to begin. Most lines of Rust code end with a +`;`. + +### Compiling and Running Are Separate Steps + +In "Writing and Running a Rust Program", we showed you how to run a newly +created program. We'll break that process down and examine each step now. + +Before running a Rust program, you have to compile it. You can use the Rust +compiler by entering the `rustc` command and passing it the name of your source +file, like this: + +```bash +$ rustc main.rs +``` + +If you come from a C or C++ background, you'll notice that this is similar to +`gcc` or `clang`. After compiling successfully, Rust should output a binary +executable, which you can see on Linux or OSX by entering the `ls` command in +your shell as follows: + +```bash +$ ls +main main.rs +``` + +On Windows, you'd enter: + +```bash +$ dir +main.exe main.rs +``` + +This shows we have two files: the source code, with the `.rs` extension, and the +executable (`main.exe` on Windows, `main` everywhere else). All that's left to +do from here is run the `main` or `main.exe` file, like this: + +```bash +$ ./main # or main.exe on Windows +``` + +If *main.rs* were your "Hello, world!" program, this would print `Hello, +world!` to your terminal. + +If you come from a dynamic language like Ruby, Python, or JavaScript, you may +not be used to compiling and running a program being separate steps. Rust is an +*ahead-of-time compiled* language, which means that you can compile a program, +give it to someone else, and they can run it even without having Rust +installed. If you give someone a `.rb`, `.py`, or `.js` file, on the other +hand, they need to have a Ruby, Python, or JavaScript implementation installed +(respectively), but you only need one command to both compile and run your +program. Everything is a tradeoff in language design. + +Just compiling with `rustc` is fine for simple programs, but as your project +grows, you'll want to be able to manage all of the options your project has +and make it easy to share your code with other people and projects. Next, we'll +introduce you to a tool called Cargo, which will help you write real-world Rust +programs. + +## Hello, Cargo! + +Cargo is Rust’s build system and package manager, and Rustaceans use Cargo to +manage their Rust projects because it makes a lot of tasks easier. For example, +Cargo takes care of building your code, downloading the libraries your code +depends on, and building those libraries. We call libraries your code needs +‘dependencies’ since your code depends on them. + +The simplest Rust programs, like the one we've written so far, don’t have any +dependencies, so right now, you'd only be using the part of Cargo that can take +care of building your code. As you write more complex Rust programs, you’ll +want to add dependencies, and if you start off using Cargo, that will be a lot +easier to do. + +As the vast, vast majority of Rust projects use Cargo, we will assume that +you’re using it for the rest of the book. Cargo comes installed with Rust +itself, if you used the official installers as covered in the Installation +chapter. If you installed Rust through some other means, you can check if you +have Cargo installed by typing the following into your terminal: + +```bash +$ cargo --version +``` + +If you see a version number, great! If you see an error like `command not +found`, then you should look at the documentation for the way you installed +Rust to determine how to install Cargo separately. + +### Creating a Project with Cargo + +Let's create a new project using Cargo and look at how it differs from our +project in `hello_world`. Go back to your projects directory (or wherever you +decided to put your code): + +```bash +$ cd ~/projects +``` + +And then run: + +```bash +$ cargo new hello_cargo --bin +$ cd hello_cargo +``` + +We passed the `--bin` argument to `cargo new` because our goal is to make an +executable application, as opposed to a library. Executables are often called +*binaries* (as in `/usr/bin`, if you’re on a Unix system). `hello_cargo` is the +name we've chosen for our project, and Cargo creates its files in a directory +of the same name that we can then go into. + +If we list the files in the `hello_cargo` directory, we can see that Cargo has +generated two files and one directory for us: a `Cargo.toml` and a *src* +directory with a *main.rs* file inside. It has also initialized a new `git` +repository in the `hello_cargo` directory for us; you can change this to use a +different version control system, or no version control system, by using the +`--vcs` flag. + +Open up `Cargo.toml` in your text editor of choice. It should look something +like this: + +```toml +[package] +name = "hello_cargo" +version = "0.1.0" +authors = ["Your Name "] + +[dependencies] +``` + +This file is in the *[TOML]* (Tom's Obvious, Minimal Language) format. TOML is +similar to INI but has some extra goodies and is used as Cargo’s +configuration format. + +[TOML]: https://github.com/toml-lang/toml + +The first line, `[package]`, is a section heading that indicates that the +following statements are configuring a package. As we add more information to +this file, we’ll add other sections. + +The next three lines set the three bits of configuration that Cargo needs to +see in order to know that it should compile your program: its name, what +version it is, and who wrote it. Cargo gets your name and email information +from your environment. If it’s not correct, go ahead and fix that and save the +file. + +The last line, `[dependencies]`, is the start of a section for you to list any +crates that your project will depend on so that Cargo knows to download and +compile those too. We won't need any other crates for this project, but we will +in the guessing game tutorial in the next chapter. + +Now let's look at `src/main.rs`: + +```rust +fn main() { + println!("Hello, world!"); +} +``` + +Cargo has generated a "Hello World!" for you, just like the one we wrote +earlier! So that part is the same. The differences between our previous project +and the project generated by Cargo that we've seen so far are: + +1. Our code goes in the `src` directory +2. The top level contains a `Cargo.toml` configuration file + +Cargo expects your source files to live inside the *src* directory so that the +top-level project directory is just for READMEs, license information, +configuration files, and anything else not related to your code. In this way, +using Cargo helps you keep your projects nice and tidy. There's a place for +everything, and everything is in its place. + +If you started a project that doesn't use Cargo, as we did with our project in +the `hello_world` directory, you can convert it to a project that does use +Cargo by moving your code into the `src` directory and creating an appropriate +`Cargo.toml`. + +### Building and Running a Cargo Project + +Now let's look at what's different about building and running your Hello World +program through Cargo! To do so, enter the following commands: + +```bash +$ cargo build + Compiling hello_cargo v0.1.0 (file:///home/yourname/projects/hello_cargo) +``` + +This should have created an executable file in `target/debug/hello_cargo` (or `target/debug/hello_cargo.exe` on Windows), which you can run with this command: + +```bash +$ ./target/debug/hello_cargo # or ./target/debug/hello_cargo.exe on Windows +Hello, world! +``` + +Bam! If all goes well, `Hello, world!` should print to the terminal once more. + +Running `cargo build` for the first time also causes Cargo to create a new file +at the top level called *Cargo.lock*, which looks like this: + +```toml +[root] +name = "hello_cargo" +version = "0.1.0" +``` + +Cargo uses the *Cargo.lock* file to keep track of dependencies in your +application. This project doesn't have dependencies, so the file is a bit +sparse. Realistically, you won't ever need to touch this file yourself; just +let Cargo handle it. + +We just built a project with `cargo build` and ran it with +`./target/debug/hello_cargo`, but we can actually do both in one step with +`cargo run` as follows: + +```bash +$ cargo run + Running `target/debug/hello_cargo` +Hello, world! +``` + +Notice that this time, we didn't see the output that Cargo was compiling +`hello_cargo`. Cargo figured out that the files haven’t changed, so it just ran +the binary. If you had modified your source code, Cargo would have rebuilt the +project before running it, and you would have seen something like this: + +```bash +$ cargo run + Compiling hello_cargo v0.1.0 (file:///home/yourname/projects/hello_cargo) + Running `target/debug/hello_cargo` +Hello, world! +``` + +So a few more differences we've now seen: + +3. Instead of using `rustc`, build a project using `cargo build` (or build and run it in one step with `cargo run`) +4. Instead of the result of the build being put in the same directory as our code, Cargo will put it in the `target/debug` directory. + +### Building for Release + +When your project is finally ready for release, you can use `cargo build +--release` to compile your project with optimizations. This will create an +executable in `target/release` instead of `target/debug`. These optimizations +make your Rust code run faster, but turning them on makes your program take +longer to compile. This is why there are two different profiles: one for +development when you want to be able to rebuild quickly and often, and one for +building the final program you’ll give to a user that won't be rebuilt and +that we want to run as fast as possible. If you're benchmarking the running +time of your code, be sure to run `cargo build --release` and benchmark with +the executable in `target/release`. + +### Cargo as Convention + +With simple projects, Cargo doesn't provide a whole lot of value over just +using `rustc`, but it will prove its worth as you continue. With complex +projects composed of multiple crates, it’s much easier to let Cargo coordinate +the build. With Cargo, you can just run `cargo build`, and it should work the +right way. Even though this project is simple, it now uses much of the real +tooling you’ll use for the rest of your Rust career. In fact, you can get +started with virtually all Rust projects you might find that you want to work +on with the following commands: + +```bash +$ git clone someurl.com/someproject +$ cd someproject +$ cargo build +``` + +> Note: If you want to look at Cargo in more detail, check out the official +[Cargo guide], which covers all of its features. + +[Cargo guide]: http://doc.crates.io/guide.html diff --git a/src/ch01-03-hello-world.md b/src/ch01-03-hello-world.md deleted file mode 100644 index 75c67b87c..000000000 --- a/src/ch01-03-hello-world.md +++ /dev/null @@ -1,395 +0,0 @@ -# Hello, world! - -Now that you have Rust installed, let’s write your first Rust program. It's -traditional when learning a new language to write a little program to print the -text “Hello, world!” to the screen, and in this section, we'll follow that -tradition. - -The nice thing about starting with such a simple program is that you can -quickly verify that your compiler is installed, and that it's working properly. -Printing information to the screen is also just a pretty common thing to do, so -practicing it early on is good. - -> Note: This book assumes basic familiarity with the command line. Rust itself -> makes no specific demands about your editing, tooling, or where your code -> lives, so if you prefer an IDE to the command line, that's an option. - -## Creating a Project File - -First, make a file to put your Rust code in. Rust doesn't care where your code -lives, but for this book, I suggest making a *projects* directory in your home -directory, and keeping all your projects there. Open a terminal and enter the -following commands to make a directory for this particular project: - -```bash -$ mkdir ~/projects -$ cd ~/projects -$ mkdir hello_world -$ cd hello_world -``` - -> Note: If you’re on Windows and not using PowerShell, the `~` may not work. -> Consult the documentation for your shell for more details. - -## Writing and Running a Rust Program - -Next, make a new source file and call it *main.rs*. Rust files always end -in a *.rs* extension. If you’re using more than one word in your filename, use -an underscore to separate them; for example, you'd use *hello_world.rs* rather -than *helloworld.rs*. - -Now open the *main.rs* file you just created, and type the following code: - -```rust -fn main() { - println!("Hello, world!"); -} -``` - -Save the file, and go back to your terminal window. On Linux or OSX, enter the -following commands: - -```bash -$ rustc main.rs -$ ./main -Hello, world! -``` - -In Windows, just replace `main` with `main.exe`. Regardless of your operating -system, you should see the string `Hello, world!` print to the terminal. If you -did, then congratulations! You've officially written a Rust program. That makes -you a Rust programmer! Welcome. - -## Anatomy of a Rust Program - -Now, let’s go over what just happened in your "Hello, world!" program in -detail. Here's the first piece of the puzzle: - -```rust -fn main() { - -} -``` - -These lines define a *function* in Rust. The `main` function is special: it's -the beginning of every Rust program. The first line says, “I’m declaring a -function named `main` that takes no arguments and returns nothing.” If there -were arguments, they would go inside the parentheses (`(` and `)`), and because -we aren’t returning anything from this function, we can omit the return type -entirely. - -Also note that the function body is wrapped in curly braces (`{` and `}`). Rust -requires these around all function bodies. It's considered good style to put -the opening curly brace on the same line as the function declaration, with one -space in between. - -Inside the `main()` function: - -```rust - println!("Hello, world!"); -``` - -This line does all of the work in this little program: it prints text to the -screen. There are a number of details that are important here. The first is -that it’s indented with four spaces, not tabs. - -The second important part is the `println!()` line. This is calling a Rust -*[macro]*, which is how metaprogramming is done in Rust. If it were calling a -function instead, it would look like this: `println()` (without the !). We'll -discuss Rust macros in more detail later, but for now you just need to -know that when you see a `!` that means that you’re calling a macro instead of -a normal function. - -[macro]: macros.html - -Next is `"Hello, world!"` which is a *string*. We pass this string as an -argument to `println!`, which prints the string to the screen. Easy enough! - -The line ends with a semicolon (`;`). Rust is an *expression oriented* -language, which means that most things are expressions, rather than statements. -The `;` indicates that this expression is over, and the next one is ready to -begin. Most lines of Rust code end with a `;`. - -## Compiling and Running Are Separate Steps - -In "Writing and Running a Rust Program", we showed you how to run a newly -created program. We'll break that process down and examine each step now. - -Before running a Rust program, you have to compile it. You can use the Rust -compiler by entering the `rustc` command and passing it the name of your source -file, like this: - -```bash -$ rustc main.rs -``` - -If you come from a C or C++ background, you'll notice that this is similar to -`gcc` or `clang`. After compiling successfully, Rust should output a binary -executable, which you can see on Linux or OSX by entering the `ls` command in -your shell as follows: - -```bash -$ ls -main main.rs -``` - -On Windows, you'd enter: - -```bash -$ dir -main.exe main.rs -``` - -This shows we have two files: the source code, with an `.rs` extension, and the -executable (`main.exe` on Windows, `main` everywhere else). All that's left to -do from here is run the `main` or `main.exe` file, like this: - -```bash -$ ./main # or main.exe on Windows -``` - -If *main.rs* were your "Hello, world!" program, this would print `Hello, -world!` to your terminal. - -If you come from a dynamic language like Ruby, Python, or JavaScript, you may -not be used to compiling and running a program being separate steps. Rust is an -*ahead-of-time compiled* language, which means that you can compile a program, -give it to someone else, and they can run it even without Rust installed. If -you give someone a `.rb` or `.py` or `.js` file, on the other hand, they need -to have a Ruby, Python, or JavaScript implementation installed (respectively), -but you only need one command to both compile and run your program. Everything -is a tradeoff in language design. - -Just compiling with `rustc` is fine for simple programs, but as your project -grows, you'll want to be able to manage all of the options your project has, -and make it easy to share your code with other people and projects. Next, I'll -introduce you to a tool called Cargo, which will help you write real-world Rust -programs. - -# Hello, Cargo! - -Cargo is Rust’s build system and package manager, and Rustaceans use Cargo to -manage their Rust projects. Cargo manages three things: building your code, -downloading the libraries your code depends on, and building those libraries. -We call libraries your code needs ‘dependencies’ since your code depends on -them. - -The simplest Rust programs don’t have any dependencies, so right now, you'd -only use the first part of its functionality. As you write more complex Rust -programs, you’ll want to add dependencies, and if you start off using Cargo, -that will be a lot easier to do. - -As the vast, vast majority of Rust projects use Cargo, we will assume that -you’re using it for the rest of the book. Cargo comes installed with Rust -itself, if you used the official installers. If you installed Rust through some -other means, you can check if you have Cargo installed by typing: - -```bash -$ cargo --version -``` - -Into a terminal. If you see a version number, great! If you see an error like -‘`command not found`’, then you should look at the documentation for the system -in which you installed Rust, to determine if Cargo is separate. - -## Converting to Cargo - -Let’s convert the Hello World program to Cargo. To Cargo-fy a project, you need -to do three things: - -1. Put your source file in the right directory. -2. Get rid of the old executable (`main.exe` on Windows, `main` everywhere - else). -3. Make a Cargo configuration file. - -Let's get started! - -### Creating a Source Directory and Removing the Old Executable - -First, go back to your terminal, move to your *hello_world* directory, and -enter the following commands: - -```bash -$ mkdir src -$ mv main.rs src/main.rs # or 'move main.rs src/main.rs' on Windows -$ rm main # or 'del main.exe' on Windows -``` - -Cargo expects your source files to live inside a *src* directory, so do that -first. This leaves the top-level project directory (in this case, -*hello_world*) for READMEs, license information, and anything else not related -to your code. In this way, using Cargo helps you keep your projects nice and -tidy. There's a place for everything, and everything is in its place. - -Now, move *main.rs* into the *src* directory, and delete the compiled file you -created with `rustc`. As usual, replace `main` with `main.exe` if you're on -Windows. - -This example retains `main.rs` as the source filename because it's creating an -executable. If you wanted to make a library instead, you'd name the file -`lib.rs`. This convention is used by Cargo to successfully compile your -projects, but it can be overridden if you wish. - -### Creating a Configuration File - -Next, create a new file inside your *hello_world* directory, and call it -`Cargo.toml`. - -Make sure to capitalize the `C` in `Cargo.toml`, or Cargo won't know what to do -with the configuration file. - -This file is in the *[TOML]* (Tom's Obvious, Minimal Language) format. TOML is -similar to INI, but has some extra goodies, and is used as Cargo’s -configuration format. - -[TOML]: https://github.com/toml-lang/toml - -Inside this file, type the following information: - -```toml -[package] - -name = "hello_world" -version = "0.1.0" -authors = [ "Your name " ] -``` - -The first line, `[package]`, indicates that the following statements are -configuring a package. As we add more information to this file, we’ll add other -sections, but for now, we just have the package configuration. - -The other three lines set the three bits of configuration that Cargo needs to -know to compile your program: its name, what version it is, and who wrote it. - -Once you've added this information to the *Cargo.toml* file, save it to finish -creating the configuration file. - -## Building and Running a Cargo Project - -With your *Cargo.toml* file in place in your project's root directory, you -should be ready to build and run your Hello World program! To do so, enter the -following commands: - -```bash -$ cargo build - Compiling hello_world v0.1.0 (file:///home/yourname/projects/hello_world) -$ ./target/debug/hello_world -Hello, world! -``` - -Bam! If all goes well, `Hello, world!` should print to the terminal once more. - -You just built a project with `cargo build` and ran it with -`./target/debug/hello_world`, but you can actually do both in one step with -`cargo run` as follows: - -```bash -$ cargo run - Running `target/debug/hello_world` -Hello, world! -``` - -Notice that this example didn’t re-build the project. Cargo figured out that -the file hasn’t changed, and so it just ran the binary. If you'd modified your -source code, Cargo would have rebuilt the project before running it, and you -would have seen something like this: - -```bash -$ cargo run - Compiling hello_world v0.1.0 (file:///home/yourname/projects/hello_world) - Running `target/debug/hello_world` -Hello, world! -``` - -Cargo checks to see if any of your project’s files have been modified, and only -rebuilds your project if they’ve changed since the last time you built it. - -With simple projects, Cargo doesn't bring a whole lot over just using `rustc`, -but it will become useful in the future. With complex projects composed of multiple -crates, it’s much easier to let Cargo coordinate the build. With Cargo, you can -just run `cargo build`, and it should work the right way. - -## Building for Release - -When your project is finally ready for release, you can use `cargo build ---release` to compile your project with optimizations. These optimizations make -your Rust code run faster, but turning them on makes your program take longer -to compile. This is why there are two different profiles, one for development, -and one for building the final program you’ll give to a user. - -Running this command also causes Cargo to create a new file called -*Cargo.lock*, which looks like this: - -```toml -[root] -name = "hello_world" -version = "0.1.0" -``` - -Cargo uses the *Cargo.lock* file to keep track of dependencies in your -application. This is the Hello World project's *Cargo.lock* file. This project -doesn't have dependencies, so the file is a bit sparse. Realistically, you -won't ever need to touch this file yourself; just let Cargo handle it. - -That’s it! If you've been following along, you should have successfully built -`hello_world` with Cargo. - -Even though the project is simple, it now uses much of the real tooling you’ll -use for the rest of your Rust career. In fact, you can expect to start -virtually all Rust projects with some variation on the following commands: - -```bash -$ git clone someurl.com/foo -$ cd foo -$ cargo build -``` - -## Making A New Cargo Project the Easy Way - -You don’t have to go through that previous process every time you want to start -a new project! Cargo can quickly make a bare-bones project directory that you -can start developing in right away. - -To start a new project with Cargo, enter `cargo new` at the command line: - -```bash -$ cargo new hello_world --bin -``` - -This command passes `--bin` because the goal is to get straight to making an -executable application, as opposed to a library. Executables are often called -*binaries* (as in `/usr/bin`, if you’re on a Unix system). - -Cargo has generated two files and one directory for us: a `Cargo.toml` and a -*src* directory with a *main.rs* file inside. These should look familiar, -they’re exactly what we created by hand, above. - -This output is all you need to get started. First, open `Cargo.toml`. It should -look something like this: - -```toml -[package] - -name = "hello_world" -version = "0.1.0" -authors = ["Your Name "] -``` - -Cargo has populated *Cargo.toml* with reasonable defaults based on the arguments -you gave it and your `git` global configuration. You may notice that Cargo has -also initialized the `hello_world` directory as a `git` repository. - -Here’s what should be in `src/main.rs`: - -```rust -fn main() { - println!("Hello, world!"); -} -``` - -Cargo has generated a "Hello World!" for you, and you’re ready to start coding! - -> Note: If you want to look at Cargo in more detail, check out the official [Cargo -guide], which covers all of its features. - -[Cargo guide]: http://doc.crates.io/guide.html diff --git a/src/ch01-04-design.md b/src/ch01-04-design.md deleted file mode 100644 index 139ee1fa1..000000000 --- a/src/ch01-04-design.md +++ /dev/null @@ -1 +0,0 @@ -# The Design of Rust diff --git a/src/ch02-00-guessing-game-tutorial.md b/src/ch02-00-guessing-game-tutorial.md new file mode 100644 index 000000000..0deffef99 --- /dev/null +++ b/src/ch02-00-guessing-game-tutorial.md @@ -0,0 +1,966 @@ +# Guessing Game + +Let's jump into Rust with a hands-on project! We’ll implement a classic +beginner programming problem: the guessing game. Here’s how it works: Our +program will generate a random integer between one and a hundred. It will then +prompt us to enter a guess. Upon entering our guess, it will tell us if we’re +too low or too high. Once we guess correctly, it will congratulate us. + +## Set up + +Let’s set up a new project. Go to your projects directory, and create a new project using Cargo. + +```bash +$ cd ~/projects +$ cargo new guessing_game --bin +$ cd guessing_game +``` + +We pass the name of our project to `cargo new`, then the `--bin` flag, since +we’re going to be making another binary like in Chapter 1. + +Take a look at the generated `Cargo.toml`: + +```toml +[package] +name = "guessing_game" +version = "0.1.0" +authors = ["Your Name "] + +[dependencies] +``` + +If the authors information that Cargo got from your environment is not correct, +go ahead and fix that. + +And as we saw in the last chapter, `cargo new` generates a ‘Hello, world!’ for +us. Check out `src/main.rs`: + +```rust +fn main() { + println!("Hello, world!"); +} +``` + +Let’s try compiling what Cargo gave us and running it in the same step, using the `cargo run` command: + +```bash +$ cargo run + Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game) + Running `target/debug/guessing_game` +Hello, world! +``` + +Great! The `run` command comes in handy when you need to rapidly iterate on a +project. Our game is such a project: we want to quickly test each +iteration before moving on to the next one. + +Now open up your `src/main.rs` again. We’ll be writing all of our code in this +file. + +## Processing a Guess + +Let’s get to it! The first thing we need to do for our guessing game is +allow our player to input a guess. Put this in your `src/main.rs`: + +```rust,ignore +use std::io; + +fn main() { + println!("Guess the number!"); + + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin().read_line(&mut guess) + .expect("Failed to read line"); + + println!("You guessed: {}", guess); +} +``` + +There’s a lot here! Let’s go over it, bit by bit. + +```rust,ignore +use std::io; +``` + +We’ll need to take user input and then print the result as output. As such, we +need the `io` library from the standard library. Rust only imports a few things +by default into every program, [the ‘prelude’][prelude]. If it’s not in the +prelude, you’ll have to `use` it directly. Using the `std::io` library gets +you a number of useful `io`-related things, so that's what we've done here. + +[prelude]: ../std/prelude/index.html + +```rust,ignore +fn main() { +``` + +As you’ve seen in Chapter 1, the `main()` function is the entry point into the +program. The `fn` syntax declares a new function, the `()`s indicate that +there are no arguments, and `{` starts the body of the function. + +```rust,ignore +println!("Guess the number!"); + +println!("Please input your guess."); +``` + +We previously learned in Chapter 1 that `println!()` is a macro that +prints a string to the screen. + +### Variable Bindings + +```rust,ignore +let mut guess = String::new(); +``` + +Now we’re getting interesting! There’s a lot going on in this little line. +The first thing to notice is that this is a let statement, which is +used to create what are called ‘variable bindings’. Here's an example: + +```rust,ignore +let foo = bar; +``` + +This will create a new binding named `foo`, and bind it to the value `bar`. In +many languages, this is called a ‘variable’, but Rust’s variable bindings have +a few tricks up their sleeves. + +For example, they’re immutable by default. That’s why our example +uses `mut`: it makes a binding mutable, rather than immutable. + +```rust +let foo = 5; // immutable. +let mut bar = 5; // mutable +``` + +Oh, and `//` will start a comment, until the end of the line. Rust ignores +everything in comments. + +So now we know that `let mut guess` will introduce a mutable binding named +`guess`, but we have to look at the other side of the `=` for what it’s +bound to: `String::new()`. + +`String` is a string type, provided by the standard library. A +[`String`][string] is a growable, UTF-8 encoded bit of text. + +[string]: ../std/string/struct.String.html + +The `::new()` syntax uses `::` because this is an ‘associated function’ of +a particular type. That is to say, it’s associated with `String` itself, +rather than a particular instance of a `String`. Some languages call this a +‘static method’. + +This function is named `new()`, because it creates a new, empty `String`. +You’ll find a `new()` function on many types, as it’s a common name for making +a new value of some kind. + +Let’s move forward: + +```rust,ignore +io::stdin().read_line(&mut guess) + .expect("Failed to read line"); +``` + +Let’s go through this together bit-by-bit. The first line has two parts. Here’s +the first: + +```rust,ignore +io::stdin() +``` + +Remember how we `use`d `std::io` on the first line of the program? We’re now +calling an associated function on it. If we didn’t `use std::io`, we could +have written this line as `std::io::stdin()`. + +This particular function returns a handle to the standard input for your +terminal. More specifically, a [std::io::Stdin][iostdin]. + +[iostdin]: ../std/io/struct.Stdin.html + +The next part will use this handle to get input from the user: + +```rust,ignore +.read_line(&mut guess) +``` + +Here, we call the [`read_line()`][read_line] method on our handle. We’re also +passing one argument to `read_line()`: `&mut guess`. + +[read_line]: ../std/io/struct.Stdin.html#method.read_line + +Remember how we bound `guess` above? We said it was mutable. However, +`read_line` doesn’t take a `String` as an argument: it takes a `&mut String`. +The `&` is the feature of Rust called a ‘reference’, which allows you to have +multiple ways to access one piece of data in order to reduce copying. +References are a complex feature, as one of Rust’s major selling points is how +safe and easy it is to use references. We don’t need to know a lot of those +details to finish our program right now, though; Chapter XX will cover them in +more detail. For now, all we need to know is that like `let` bindings, +references are immutable by default. Hence, we need to write `&mut guess`, +rather than `&guess`. + +Why does `read_line()` take a mutable reference to a string? Its job is +to take what the user types into standard input and place that into a +string. So it takes that string as an argument, and in order to add +the input, that string needs to be mutable. + +But we’re not quite done with this line of code, though. While it’s +a single line of text, it’s only the first part of the single logical line of +code. This is the second part of the line: + +```rust,ignore +.expect("Failed to read line"); +``` + +When you call a method with the `.foo()` syntax, you may introduce a newline +and other whitespace. This helps you split up long lines. We _could_ have +written this code as: + +```rust,ignore +io::stdin().read_line(&mut guess).expect("failed to read line"); +``` + +But that gets hard to read. So we’ve split it up, two lines for two method +calls. + +### The `Result` Type + +We already talked about `read_line()`, but what about `expect()`? Well, +we already mentioned that `read_line()` puts what the user types into the `&mut +String` we pass it. But it also returns a value: in this case, an +[`io::Result`][ioresult]. Rust has a number of types named `Result` in its +standard library: a generic [`Result`][result], and then specific versions for +sub-libraries, like `io::Result`. + +[ioresult]: ../std/io/type.Result.html +[result]: ../std/result/enum.Result.html + +The purpose of these `Result` types is to encode error handling information. +Values of the `Result` type, like any type, have methods defined on them. In +this case, `io::Result` has an [`expect()` method][expect] that takes a value +it’s called on, and if it isn’t a successful result, will cause our program to +crash and display the message that we passed as an argument to `expect()`. + +[expect]: ../std/result/enum.Result.html#method.expect + +If we don't call this method, our program will compile, but we’ll get a warning: + +```bash +$ cargo build + Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game) +src/main.rs:10:5: 10:39 warning: unused result which must be used, +#[warn(unused_must_use)] on by default +src/main.rs:10 io::stdin().read_line(&mut guess); + ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +``` + +Rust warns us that we haven’t used the `Result` value. This warning comes from +a special annotation that `io::Result` has. Rust is trying to tell you that you +haven’t handled a possible error. The right way to suppress the error is to +actually write error handling. Luckily, if we want to crash if there’s a +problem, we can use `expect()`. If we can recover from the error somehow, we’d +do something else, but we’ll save that for a future project. + +### `println!()` Placeholders + +There’s only one line of this first example left, aside from the closing curly +brace: + +```rust,ignore + println!("You guessed: {}", guess); +} +``` + +This prints out the string we saved our input in. The `{}`s are a placeholder: +think of `{}` as little crab pincers, holding a value in place. The first `{}` +holds the first value after the format string, the second set holds the second +value, and so on. Printing out multiple values in one call to `println!()` would then look like this: + +```rust +let x = 5; +let y = 10; + +println!("x and y: {} and {}", x, y); +``` + +Which would print out "x and y: 5 and 10". + +Anyway, back to our guessing game. We can run what we have with `cargo run`: + +```bash +$ cargo run + Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game) + Running `target/debug/guessing_game` +Guess the number! +Please input your guess. +6 +You guessed: 6 +``` + +All right! Our first part is done: we can get input from the keyboard and then +print it back out. + +## Generating a secret number + +Next, we need to generate a secret number. Rust does not yet include random +number functionality in its standard library. The Rust team does, however, +provide a [`rand` crate][randcrate]. A ‘crate’ is a package of Rust code. +We’ve been building a ‘binary crate’, which is an executable. `rand` is a +‘library crate’, which contains code that’s intended to be used with other +programs. + +[randcrate]: https://crates.io/crates/rand + +Using external crates is where Cargo really shines. Before we can write +the code using `rand`, we need to modify our `Cargo.toml`. Open it up, and +add this line at the bottom beneath the `[dependencies]` section header that +Cargo created for you: + +```toml +[dependencies] + +rand = "0.3.14" +``` + +The `[dependencies]` section of `Cargo.toml` is like the `[package]` section: +everything that follows the section heading is part of that section, until +another section starts. Cargo uses the dependencies section to know what +dependencies on external crates you have and what versions of those crates you +require. In this case, we’ve specified the `rand` crate with the semantic +version specifier `0.3.14`. Cargo understands [Semantic Versioning][semver], a +standard for writing version numbers. A bare number like above is actually +shorthand for `^0.3.14`, which means "any version that has a public API +compatible with version 0.3.14". + +[semver]: http://semver.org + +Now, without changing any of our code, let’s build our project: + +```bash +$ cargo build + Updating registry `https://github.com/rust-lang/crates.io-index` + Downloading rand v0.3.14 + Downloading libc v0.2.14 + Compiling libc v0.2.14 + Compiling rand v0.3.14 + Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game) +``` + +You may see different versions (but they will be compatible, thanks to semver!) +and the lines may be in a different order. + +Lots of new output! Now that we have an external dependency, Cargo fetches the +latest versions of everything from the *registry*, which is a copy of data from +[Crates.io][cratesio]. Crates.io is where people in the Rust ecosystem +post their open source Rust projects for others to use. + +[cratesio]: https://crates.io + +After updating the registry, Cargo checks our `[dependencies]` and downloads +any we don’t have yet. In this case, while we only said we wanted to depend on +`rand`, we’ve also grabbed a copy of `libc`. This is because `rand` depends on +`libc` to work. After downloading them, it compiles them and then compiles +our project. + +If we run `cargo build` again, we’ll get different output: + +```bash +$ cargo build +``` + +That’s right, no output! Cargo knows that our project has been built, that +all of its dependencies are built, and that no changes have been made. There’s +no reason to do all that stuff again. With nothing to do, it simply +exits. If we open up `src/main.rs`, make a trivial change, then save it again, +we’ll only see one line: + +```bash +$ cargo build + Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game) +``` + +What happens when next week version `v0.3.15` of the `rand` crate comes out, +with an important bugfix? While getting bugfixes is important, what if `0.3.15` +contains a regression that breaks our code? + +The answer to this problem is the `Cargo.lock` file created the first time we +ran `cargo build` that is now in your project directory. When you build your +project for the first time, Cargo figures out all of the versions that fit your +criteria then writes them to the `Cargo.lock` file. When you build your project +in the future, Cargo will see that the `Cargo.lock` file exists and then use +that specific version rather than do all the work of figuring out versions +again. This lets you have a repeatable build automatically. In other words, +we’ll stay at `0.3.14` until we explicitly upgrade, and so will anyone who we +share our code with, thanks to the lock file. + +What about when we _do_ want to use `v0.3.15`? Cargo has another command, +`update`, which says ‘ignore the `Cargo.lock` file and figure out all the +latest versions that fit what we’ve specified in `Cargo.toml`. If that works, +write those versions out to the lock file’. But by default, Cargo will only +look for versions larger than `0.3.0` and smaller than `0.4.0`. If we want to +move to `0.4.x`, we’d have to update what is in the `Cargo.toml` file. When we +do, the next time we `cargo build`, Cargo will update the index and re-evaluate +our `rand` requirements. + +There’s a lot more to say about [Cargo][doccargo] and [its +ecosystem][doccratesio] that we will get into in Chapter XX, but for now, +that’s all we need to know. Cargo makes it really easy to re-use libraries, so +Rustaceans are able to write smaller projects which are assembled out of a +number of sub-packages. + +[doccargo]: http://doc.crates.io +[doccratesio]: http://doc.crates.io/crates-io.html + +Let’s get on to actually _using_ `rand`. Here’s our next step: + +```rust,ignore +extern crate rand; + +use std::io; +use rand::Rng; + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1, 101); + + println!("The secret number is: {}", secret_number); + + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin().read_line(&mut guess) + .expect("failed to read line"); + + println!("You guessed: {}", guess); +} +``` + +The first thing we’ve done is change the first line. It now says `extern crate +rand`. Because we declared `rand` in our `[dependencies]`, we can now put +`extern crate` in our code to let Rust know we’ll be making use of that +dependency. This also does the equivalent of a `use rand;` as well, so we can +call anything in the `rand` crate by prefixing it with `rand::`. + +Next, we added another `use` line: `use rand::Rng`. We’re going to use a +method in a moment, and it requires that `Rng` be in scope to work. The basic +idea is this: methods are defined on something called ‘traits’, and for the +method to work, it needs the trait to be in scope. For more about the +details, read the traits section in Chapter XX. + +There are two other lines we added, in the middle: + +```rust,ignore +let secret_number = rand::thread_rng().gen_range(1, 101); + +println!("The secret number is: {}", secret_number); +``` + +We use the `rand::thread_rng()` function to get a copy of the random number +generator, which is local to the particular thread of execution +we’re in. Because we put `use rand::Rng` above, the random number generator has +a `gen_range()` method available. This method takes two numbers as arguments +and generates a random number between them. It’s inclusive on the lower bound +but exclusive on the upper bound, so we need `1` and `101` to ask for a number +ranging from one to a hundred. + +The second line prints out the secret number. This is useful while +we’re developing our program to let us easily test it out, but we’ll be +deleting it for the final version. It’s not much of a game if it prints out +the answer when you start it up! + +Try running our new program a few times: + +```bash +$ cargo run + Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game) + Running `target/debug/guessing_game` +Guess the number! +The secret number is: 7 +Please input your guess. +4 +You guessed: 4 +$ cargo run + Running `target/debug/guessing_game` +Guess the number! +The secret number is: 83 +Please input your guess. +5 +You guessed: 5 +``` + +You should get different random numbers, and they should all be between 1 and +100. Great job! Next up: comparing our guess to the secret number. + +## Comparing guesses + +Now that we’ve got user input, let’s compare our guess to the secret number. +Here’s part of our next step. It won't quite compile yet though: + +```rust,ignore +extern crate rand; + +use std::io; +use std::cmp::Ordering; +use rand::Rng; + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1, 101); + + println!("The secret number is: {}", secret_number); + + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin().read_line(&mut guess) + .expect("failed to read line"); + + println!("You guessed: {}", guess); + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => println!("You win!"), + } +} +``` + +A few new bits here. The first is another `use`. We bring a type called +`std::cmp::Ordering` into scope. Then we add five new lines at the bottom that +use that type: + +```rust,ignore +match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => println!("You win!"), +} +``` + +The `cmp()` method can be called on anything that can be compared, and it +takes a reference to the thing you want to compare it to. It returns the +`Ordering` type we `use`d earlier. We use a [`match`][match] statement to +determine exactly what kind of `Ordering` it is. `Ordering` is an +[`enum`][enum], short for ‘enumeration’, which looks like this: + +```rust +enum Foo { + Bar, + Baz, +} +``` + +[match]: match.html +[enum]: enums.html + +With this definition, anything of type `Foo` can be either a +`Foo::Bar` or a `Foo::Baz`. We use the `::` to indicate the +namespace for a particular `enum` variant. + +The [`Ordering`][ordering] `enum` has three possible variants: `Less`, `Equal`, +and `Greater`. The `match` statement takes a value of a type and lets you +create an ‘arm’ for each possible value. An arm is made up of a pattern and the +code that we should execute if the pattern matches the value of the type. Since +we have three types of `Ordering`, we have three arms: + +```rust,ignore +match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => println!("You win!"), +} +``` + +[ordering]: ../std/cmp/enum.Ordering.html + +If it’s `Less`, we print `Too small!`, if it’s `Greater`, `Too big!`, and if +`Equal`, `You win!`. `match` is really useful and is used often in Rust. + +We did mention that this won’t quite compile yet, though. Let’s try it: + +```bash +$ cargo build + Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game) +src/main.rs:23:21: 23:35 error: mismatched types [E0308] +src/main.rs:23 match guess.cmp(&secret_number) { + ^~~~~~~~~~~~~~ +src/main.rs:23:21: 23:35 help: run `rustc --explain E0308` to see a detailed explanation +src/main.rs:23:21: 23:35 note: expected type `&std::string::String` +src/main.rs:23:21: 23:35 note: found type `&_` +error: aborting due to previous error +Could not compile `guessing_game`. +``` + +Whew! This is a big error. The core of it is that we have ‘mismatched types’. +Rust has a strong, static type system. However, it also has type inference. +When we wrote `let guess = String::new()`, Rust was able to infer that `guess` +should be a `String`, so it doesn’t make us write out the type. With our +`secret_number`, there are a number of types which can have a value between one +and a hundred: `i32`, a thirty-two-bit number, or `u32`, an unsigned +thirty-two-bit number, or `i64`, a sixty-four-bit number or others. So far, +that hasn’t mattered, and so Rust defaults to an `i32`. However, here, Rust +doesn’t know how to compare the `guess` and the `secret_number`. They need to +be the same type. + +Ultimately, we want to convert the `String` we read as input +into a real number type so that we can compare it to the guess numerically. We +can do that with two more lines. Here’s our new program: + +```rust,ignore +extern crate rand; + +use std::io; +use std::cmp::Ordering; +use rand::Rng; + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1, 101); + + println!("The secret number is: {}", secret_number); + + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin().read_line(&mut guess) + .expect("failed to read line"); + + let guess: u32 = guess.trim().parse() + .expect("Please type a number!"); + + println!("You guessed: {}", guess); + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => println!("You win!"), + } +} +``` + +The new two lines: + +```rust,ignore +let guess: u32 = guess.trim().parse() + .expect("Please type a number!"); +``` + +Wait a minute, didn't we already have a `guess`? We do, but Rust allows us +to ‘shadow’ the previous `guess` with a new one. This is often used in this +exact situation, where `guess` starts as a `String`, but we want to convert it +to a `u32`. Shadowing lets us re-use the `guess` name rather than forcing us +to come up with two unique names like `guess_str` and `guess` or something +else. + +We bind `guess` to an expression that looks like something we wrote earlier: + +```rust,ignore +guess.trim().parse() +``` + +Here, `guess` refers to the old `guess`, the one that was a `String` with our +input in it. The `trim()` method on `String`s will eliminate any white space at +the beginning and end of our string. This is important, as we had to press the +‘return’ key to satisfy `read_line()`. If we type `5` and hit return, `guess` +looks like this: `5\n`. The `\n` represents ‘newline’, the enter key. `trim()` +gets rid of this, leaving our string with only the `5`. + +The [`parse()` method on strings][parse] parses a string into some kind of +number. Since it can parse a variety of numbers, we need to give Rust a hint as +to the exact type of number we want. Hence, `let guess: u32`. The colon (`:`) +after `guess` tells Rust we’re going to annotate its type. `u32` is an +unsigned, thirty-two bit integer. Rust has a number of built-in number +types, but we’ve chosen `u32`. It’s a good default choice for a small +positive number. You'll see the other number types in Chapter XX. + +[parse]: ../std/primitive.str.html#method.parse + +Just like `read_line()`, our call to `parse()` could cause an error. What if +our string contained `A👍%`? There’d be no way to convert that to a number. As +such, we’ll do the same thing we did with `read_line()`: use the `expect()` +method to crash if there’s an error. + +Let’s try our program out! + +```bash +$ cargo run + Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game) + Running `target/guessing_game` +Guess the number! +The secret number is: 58 +Please input your guess. + 76 +You guessed: 76 +Too big! +``` + +Nice! You can see we even added spaces before our guess, and it still figured +out that we guessed 76. Run the program a few times. Verify that guessing +the secret number works, as well as guessing a number too small. + +Now we’ve got most of the game working, but we can only make one guess. Let’s +change that by adding loops! + +## Looping + +The `loop` keyword gives us an infinite loop. Let’s add that in: + +```rust,ignore +extern crate rand; + +use std::io; +use std::cmp::Ordering; +use rand::Rng; + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1, 101); + + println!("The secret number is: {}", secret_number); + + loop { + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin().read_line(&mut guess) + .expect("failed to read line"); + + let guess: u32 = guess.trim().parse() + .expect("Please type a number!"); + + println!("You guessed: {}", guess); + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => println!("You win!"), + } + } +} +``` + +And try it out. But wait, didn’t we just add an infinite loop? Yup. Remember +our discussion about `parse()`? If we give a non-number answer, the program +will crash and, therefore, quit. Observe: + +```bash +$ cargo run + Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game) + Running `target/guessing_game` +Guess the number! +The secret number is: 59 +Please input your guess. +45 +You guessed: 45 +Too small! +Please input your guess. +60 +You guessed: 60 +Too big! +Please input your guess. +59 +You guessed: 59 +You win! +Please input your guess. +quit +thread 'main' panicked at 'Please type a number!: ParseIntError { kind: InvalidDigit }', src/libcore/result.rs:785 +note: Run with `RUST_BACKTRACE=1` for a backtrace. +error: Process didn't exit successfully: `target/debug/guess` (exit code: 101) +``` + +Ha! `quit` actually quits. As does any other non-number input. Well, this is +suboptimal to say the least. First, let’s actually quit when you win the game: + +```rust,ignore +extern crate rand; + +use std::io; +use std::cmp::Ordering; +use rand::Rng; + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1, 101); + + println!("The secret number is: {}", secret_number); + + loop { + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin().read_line(&mut guess) + .expect("failed to read line"); + + let guess: u32 = guess.trim().parse() + .expect("Please type a number!"); + + println!("You guessed: {}", guess); + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => { + println!("You win!"); + break; + } + } + } +} +``` + +By adding the `break` line after the `You win!`, we’ll exit the loop when we +win. Exiting the loop also means exiting the program, since the loop is the last +thing in `main()`. We have another tweak to make: when someone inputs a +non-number, we don’t want to quit, we want to ignore it. We can do that +like this: + +```rust,ignore +extern crate rand; + +use std::io; +use std::cmp::Ordering; +use rand::Rng; + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1, 101); + + println!("The secret number is: {}", secret_number); + + loop { + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin().read_line(&mut guess) + .expect("failed to read line"); + + let guess: u32 = match guess.trim().parse() { + Ok(num) => num, + Err(_) => continue, + }; + + println!("You guessed: {}", guess); + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => { + println!("You win!"); + break; + } + } + } +} +``` + +These are the lines that changed: + +```rust,ignore +let guess: u32 = match guess.trim().parse() { + Ok(num) => num, + Err(_) => continue, +}; +``` + +This is how you generally move from ‘crash on error’ to ‘actually handle the +error’: by switching from `expect()` to a `match` statement. A `Result` is the +return type of `parse()`. `Result` is an `enum` like `Ordering`, but in this +case, each variant has some data associated with it. `Ok` is a success, and +`Err` is a failure. Each contains more information: in this case, the +successfully parsed integer or an error type, respectively. When we `match` an +`Ok(num)`, that pattern sets the name `num` to the value inside the `Ok` (the +integer), and the code we run just returns that integer. In the `Err` case, we +don’t care what kind of error it is, so we just use the catch-all `_` instead +of a name. So for all errors, we run the code `continue`, which lets us move to +the next iteration of the loop, effectively ignoring the errors. + +Now we should be good! Let’s try it: + +```bash +$ cargo run + Compiling guessing_game v0.1.0 (file:///home/you/projects/guessing_game) + Running `target/guessing_game` +Guess the number! +The secret number is: 61 +Please input your guess. +10 +You guessed: 10 +Too small! +Please input your guess. +99 +You guessed: 99 +Too big! +Please input your guess. +foo +Please input your guess. +61 +You guessed: 61 +You win! +``` + +Awesome! With one tiny last tweak, we can finish the guessing game. Can you +think of what it is? That’s right, we don’t want to print out the secret +number. It was good for testing, but it kind of ruins the game. Here’s our +final source: + +```rust,ignore +extern crate rand; + +use std::io; +use std::cmp::Ordering; +use rand::Rng; + +fn main() { + println!("Guess the number!"); + + let secret_number = rand::thread_rng().gen_range(1, 101); + + loop { + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin().read_line(&mut guess) + .expect("failed to read line"); + + let guess: u32 = match guess.trim().parse() { + Ok(num) => num, + Err(_) => continue, + }; + + println!("You guessed: {}", guess); + + match guess.cmp(&secret_number) { + Ordering::Less => println!("Too small!"), + Ordering::Greater => println!("Too big!"), + Ordering::Equal => { + println!("You win!"); + break; + } + } + } +} +``` + +## Complete! + +This project showed you a lot: `let`, `match`, methods, associated +functions, using external crates, and more. + +At this point, you have successfully built the Guessing Game! Congratulations! diff --git a/src/ch03-00-common-programming-concepts-in-rust.md b/src/ch03-00-common-programming-concepts-in-rust.md new file mode 100644 index 000000000..8fa09a6b1 --- /dev/null +++ b/src/ch03-00-common-programming-concepts-in-rust.md @@ -0,0 +1,24 @@ +# Common Programming Concepts in Rust + +Let's look at concepts that appear in almost every programming language and see +how they work in Rust. Many programming languages have much in common at their +core. None of the concepts presented in this chapter are unique to Rust, but +we’ll discuss Rust’s particular syntax and conventions concerning these common +concepts. + +Specifically, we’ll be talking about variable bindings, basic types, functions, +comments, and control flow. These foundations will be in every Rust +program, and learning them early will give you a strong core to start from. + +PROD: START BOX + +Keep in mind as we get into variables and functions that the Rust language has +a set of *keywords* that have been reserved for use by the language only, much +like other languages do. This means you cannot use these words as names of +variables or functions, for example. Most of these have special meaning and we +will be using them to do various things in our Rust programs; a few have no +current functionality associated but have been reserved for functionality that +might be in the Rust language in the future. You can find a list of the +keywords in Appendix XX. + +PROD: END BOX diff --git a/src/ch03-01-up-and-running.md b/src/ch03-01-up-and-running.md deleted file mode 100644 index 0a7886b44..000000000 --- a/src/ch03-01-up-and-running.md +++ /dev/null @@ -1,11 +0,0 @@ -# Up and Running - -We’ll start our journey with Rust by talking about the absolute basics — -concepts that appear in almost every programming language. Many programming -languages have lots in common at their core. None of the concepts presented are -unique to Rust, but we’ll cover Rust’s particular syntax and conventions around -these common concepts. - -If you want to skip this section, you can, but you may end up coming back later -to find out small details. These foundations will be in every single useful -Rust program, and learning them gives us a strong core to start from. diff --git a/src/ch03-01-variable-bindings-and-mutability.md b/src/ch03-01-variable-bindings-and-mutability.md new file mode 100644 index 000000000..8854ca855 --- /dev/null +++ b/src/ch03-01-variable-bindings-and-mutability.md @@ -0,0 +1,189 @@ +## Variable Bindings and Mutability + +We mentioned in Chapter XX that by default, variable bindings are *immutable*. +This is one of many nudges that Rust's design has to encourage us to write our +code to get the most of the safety and easy concurrency that Rust has to offer. +We still have the option to make our bindings mutable, though. Let's explore +how and why Rust encourages us to favor immutability, and why we might want to +opt out of that. + +Variable bindings being immutable means that once a value is bound, you can't +change that value. To illustrate this, let's generate a new project with Cargo. +Open a terminal, and navigate to the directory you want to store your projects +in. From there, run these commands: + +```bash +$ cargo new --bin bindings +$ cd bindings +``` + +Then open *src/main.rs* and replace its code with the following: + +```rust,ignore +fn main() { + let x = 5; + println!("The value of x is: {}", x); + x = 6; + println!("The value of x is: {}", x); +} +``` + +Save and run the program using `cargo run`, and you should receive an error +message, as in this output: + +```bash +$ cargo run + Compiling bindings v0.0.1 (file:///projects/bindings) +error: re-assignment of immutable variable `x` [--explain E0384] + --> src/main.rs:4:5 +4 |> x = 6; + |> ^^^^^ +note: prior assignment occurs here + --> src/main.rs:2:9 +2 |> let x = 5; + |> ^ +``` + +This is our first example of the compiler helping us find an error in our +program! Compiler errors can be frustrating. Keep in mind that they only mean +your program isn't safely doing what you want it to do yet; they do _not_ mean +that you're not a good programmer! Experienced Rustaceans still get compiler +errors. Try to keep in mind that the Rust compiler is trying to help your +program be the very best. + +PROD: START BOX +######Extended Error Explanations + +Now that you've seen an example of a Rust error, let's look at one particularly +useful aspect of errors. Rust encourages you to seek further information on the +kind of error you've received with output like this: + +```bash +error: re-assignment of immutable variable `x` [--explain E0384] +``` + +This tells us that if we pass the `--explain` flag to `rustc` with the provided +error code, we can see an extended explanation which will try to explain common +causes of and solutions to that kind of error. Not every error has a longer +explanation, but many do. Here’s the explanation for the `E0384` error we +received: + +````bash +$ rustc --explain E0384 +This error occurs when an attempt is made to reassign an immutable variable. +For example: + +``` +fn main(){ + let x = 3; + x = 5; // error, reassignment of immutable variable +} +``` + +By default, variables in Rust are immutable. To fix this error, add the keyword +`mut` after the keyword `let` when declaring the variable. For example: + +``` +fn main(){ + let mut x = 3; + x = 5; +} +``` +```` + +These explanations can really help if you’re stuck on an error, so don't +hesitate to look up the error code. The compiler is your friend, and it's there +to help. + +PROD: END BOX + +The error includes the message `re-assigment of immutable variable` because the +program tried to assign a second value to the `x` variable. + +Getting compile-time errors when your code attempts to change a value that it +previously said was immutable is important because this very situation can lead +to bugs. If one part of your code operates on an assumption that a value it's +operating on will never change, and another part of your code changes that +value, it's possible that the first code won't do what it was designed to do. +Especially when the second piece of code only changes the value _sometimes_, +this cause of bugs can be difficult to track down after the fact. + +In Rust, our code can know that a value our code assumes won't change really +won't change, because the compiler is enforcing that guarantee for us. When +reading and writing code, we don't have to keep track in our head how and where +a value might change. This can make code easier to reason about. + +Mutability is really useful, though! Bindings are immutable only by default; +you can make them mutable by adding `mut` in front of the variable name. In +addition to telling the compiler it should allow this value to be changed, it +conveys intent to future readers of the code and says that other parts of the +code will be changing this value. + +For example, change the program you just wrote to the following: + +```rust +fn main() { + let mut x = 5; + println!("The value of x is: {}", x); + x = 6; + println!("The value of x is: {}", x); +} +``` + +Running this, we get: + +```bash +$ cargo run + Compiling bindings v0.1.0 (file:///projects/bindings) + Running `target/debug/bindings` +The value of x is: 5 +The value of x is: 6 +``` + +Using `mut`, we are allowed to change the value that `x` binds to from `5` to +`6`. You might want to make a binding mutable because it makes the code easier +to understand than an implementation that only uses immutable bindings. In +cases where you're using large data structures, mutating an instance in place +may be faster than copying and returning newly allocated instances. It all +depends on the tradeoffs you want to make in your situation. + +### Shadowing + +As we saw in the guessing game tutorial, we can declare new bindings with the +same name as a previous binding, and the new binding *shadows* the previous +binding. We say that the first binding is ‘shadowed’ by the second, which means +that the second binding's value is what you will see when you use the variable +after the second binding. This can be useful if you’d like to perform a few +transformations on a value, but have the binding be immutable after those +transformations have been completed. For example: + +```rust +fn main() { + let x = 5; + + let x = x + 1; + + let x = x * 2; + + println!("The value of x is: {}", x); +} +``` + +This program first binds `x` to a value of `5`. Then, it shadows `x` by saying +`let x =` again, taking the original value and adding `1` so that the value of +`x` is then `6`. The third `let` statement also shadows `x`, taking the +previous value and multiplying it by `2` to give `x` a final value of `12`. If +you run this, it will output: + +```bash +$ cargo run + Compiling bindings v0.1.0 (file:///projects/bindings) + Running `target/debug/bindings` +The value of x is: 12 +``` + +Shadowing is useful because it lets us modify `x` without having to make the +binding mutable. This means the compiler will still keep us from accidentally +trying to mutate `x` directly later. + +Now let's look at some of the types of values that we can bind variables to. diff --git a/src/ch03-02-data-types.md b/src/ch03-02-data-types.md new file mode 100644 index 000000000..9025a9f84 --- /dev/null +++ b/src/ch03-02-data-types.md @@ -0,0 +1,306 @@ +## Data Types + +Every value in Rust is of a certain *type*, which tells Rust what kind of data +is being given so it knows how to work with that data. In this section, we'll +look at a number of types built into the language itself split into two subsets +of Rust data types: scalar and compound. + +Something to keep in mind throughout this section: Rust is a *statically typed* +language, which means that it must know the types of all bindings at compile +time. The compiler can usually infer what type we want to use based on the +value and how we use it. When many types are possible, such as when we +converted a `String` to a numeric type using `parse()` in the guessing game +tutorial, we can add a type annotation, like this: + +```rust,ignore +let x: i32 = 5; +``` + +You will see some type annotations as we discuss the various data types. + +### Scalar Types + +A *scalar* type is one that represents a single value. There are four key +scalar types in Rust: integers, floating point numbers, booleans, and +characters. You'll likely recognize these from other programming languages, but +let's jump into how they work in Rust. + +#### Integer Types + +An *integer* is a number without a fractional component. We've used one integer +type already in this chapter, the `i32` type. This type declaration indicates +that the value it's associated with should be a signed integer (hence the `i`, +as opposed to a `u` for unsigned) for a 32-bit system. There are a number of +built-in integer types in Rust, shown in Table 3-1. + +| Length | signed | unsigned | +|--------|--------|----------| +| 8-bit | i8 | u8 | +| 16-bit | i16 | u16 | +| 32-bit | i32 | u32 | +| 64-bit | i64 | u64 | +| arch | isize | usize | + +*Table 4-1: Integer types in Rust. Each code (for example, i32) can be used to +declare the type of a value.* + +Each variant can be either signed or unsigned and has an explicit size. Signed +and unsigned merely refers to whether it is possible for the number to be +either negative or positive, meaning the number needs to have a sign with it +("signed"), or whether it will only ever be positive and can therefore be +represented without a sign ("unsigned"). It's like writing numbers on paper: +when the sign matters, a number is shown with a plus sign or minus sign, but +when it's safe to assume the number is positive, it's shown with no sign. +Signed numbers are stored using two’s complement representation (if you're +unsure what this is you can search for it online; an explanation is outside the +scope of this text). + +Finally, the `isize` and `usize` types depend on the kind of computer your +program is running on: 64-bits if you're on a 64-bit architecture, and 32-bits +if you’re on a 32-bit architecture. + +So how do you know which type of integer to use? If you're unsure, Rust's +defaults are generally good choices, and integer types default to `i32`: it’s +generally the fastest, even on 64-bit systems. The primary situation in which +you'd need to specify `isize` or `usize` is when indexing some sort of +collection, which we'll talk about in the "Arrays" section. + +#### Floating-Point Types + +Rust also has two primitive types for *floating-point numbers*, which are +numbers with decimal points. Rust's floating-point types are `f32` and `f64`, +which are 32 bits and 64 bits in size, respectively. The default type is `f64`, +as it’s roughly the same speed as `f32`, but has a larger precision. It is +possible to use an `f64` on 32 bit systems, but it will be slower than using an +`f32` on those systems. Most of the time, trading potential worse performance +for better precision is a reasonable initial choice, and you should benchmark +your code if you suspect floating-point size is a problem in your case. + +Here's an example showing floating-point numbers in action: + +```rust +fn main() { + let x = 2.0; // f64 + + let y: f32 = 3.0; // f32 +} +``` + +Floating-point numbers are represented according to the IEEE-754 standard. The +`f32` type is a single-precision float, while `f64` has double-precision. + +#### Numeric Operations + +Rust supports the usual basic mathematic operations you’d expect for all of +these number types: addition, subtraction, multiplication, division, and +modulo. This code shows how you'd use each one in a `let` statement: + +```rust +fn main() { + // addition + let sum = 5 + 10; + + // subtraction + let difference = 95.5 - 4.3; + + // multiplication + let product = 4 * 30; + + // division + let quotient = 56.7 / 32.2; + + // modulo + let remainder = 43 % 5; +} +``` + +Each expression in these statements uses a mathematical operator and evaluates +to a single value, which is then bound to a variable. + +#### The Boolean Type + +As in most other programming languages, a boolean type in Rust has two possible +values: `true` and `false`. The boolean type in Rust is specified with `bool`. +For example: + +```rust +fn main() { + let t = true; + + let f: bool = false; // with explicit type annotation +} +``` + +The main way to consume boolean values is through conditionals like an `if` +statement. We’ll cover how `if` statements work in Rust in the "Control Flow" +section of this chapter. + +#### The Character Type + +So far we’ve only worked with numbers, but Rust supports letters too. Rust’s +`char` type is the language's most primitive alphabetic type, and this code +shows one way to use it: + +```rust +fn main() { + let c = 'z'; + let z = 'ℤ'; + let heart_eyed_cat = '😻'; +} +``` + +Rust’s `char` represents a Unicode Scalar Value, which means that it can +represent a lot more than just ASCII. Accented letters, Chinese/Japanese/Korean +ideographs, emoji, and zero width spaces are all valid `char`s in Rust. Unicode +Scalar Values range from `U+0000` to `U+D7FF` and `U+E000` to `U+10FFFF` +inclusive. A "character" isn’t really a concept in Unicode, however, so your +human intuition for what a "character" is may not match up with what a `char` +is in Rust. It also means that `char`s are four bytes each. You can learn more +about Unicode Scalar Values at +*http://www.unicode.org/glossary/#unicode_scalar_value* and find a chart for +all unicode code points at *http://www.unicode.org/charts/*. + +### Compound Types + +*Compound types* can group multiple values of other types into one type. Rust +has two primitive compound types: tuples and arrays. + +#### Grouping Values into Tuples + +We’ve seen tuples already, when binding multiple values at once. A tuple is a +general way of grouping together some number of other values with distinct +types into one compound type. + +We create a tuple by writing a comma-separated list of values inside +parentheses. Each position in the tuple has a distinct type, as in this example: + +```rust +fn main() { + let tup: (i32, f64, u8) = (500, 6.4, 1); +} +``` + +Note that, unlike the examples of multiple bindings, here we bind the single +name `tup` to the entire tuple, emphasizing the fact that a tuple is considered +a single compound element. We could then use pattern matching to destructure +this tuple value, like this: + +```rust +fn main() { + let tup: (i32, f64, u8) = (500, 6.4, 1); + + let (x, y, z) = tup; + + println!("The value of y is: {}", y); +} +``` + +In this program, we first create a tuple and bind it to the name `tup`. We then +use a pattern with `let` to take `tup` and turn it into three separate +bindings, `x`, `y`, and `z`. This is called ‘destructuring’, because it breaks +the single tuple into three parts. + +Finally, we print the value of `y`, which is `6.4`. + +#### Tuple Indexing + +In addition to destructuring through pattern matching, we can also access a +tuple element directly by using a period (`.`) followed by the index of the +value we want to access. For example: + +```rust +fn main() { + let x: (i32, f64, u8) = (500, 6.4, 1); + + let five_hundred = x.0; + + let six_point_four = x.1; + + let one = x.2; +} +``` + +This program creates a tuple, `x`, and then makes new bindings to each element +by using their index. As with most programming languages, the first index in a +tuple is 0. + +### Arrays + +Another way to bind a name to a collection of multiple values is with an +*array*. Unlike a tuple, every element of an array must have the same type. +Arrays in Rust are different than arrays in some other languages because arrays +in Rust have a fixed length-- once declared, they cannot grow or shrink in size. + +In Rust, the values going into an array are written as a comma separated list +inside square brackets: + +```rust +fn main() { + let a = [1, 2, 3, 4, 5]; +} +``` + +While arrays can be useful since they are a primitive type, they aren't as +flexible as the `Vec` (short for "vector"), a similar collection type provided +by the standard library that _is_ allowed to grow or shrink in size. If you're +unsure whether to use an array or a `Vec`, you should probably go with a `Vec`, +and we'll discuss them in more detail in chapter XX. + +#### Accessing Array Elements + +An array is a single chunk of memory, allocated on the stack. We can access +elements of an array using indexing, like this: + +```rust +fn main() { + let a = [1, 2, 3, 4, 5]; + + let first = a[0]; + let second = a[1]; +} +``` + +In this example, the `first` variable will bind to `1` at index `[0]` in the +array, and `second` will bind to `2` at index `[1]` in the array. Note that +these values are copied out of the array and into `first` and `second` when the +`let` statement is called. That means if the array changes after the `let` +statements, these bindings will not, and the two variables should retain their +values. + +#### Invalid array element access + +What happens if you try to access an element of an array past the end of the +array? Say we changed our program to: + +```rust,ignore +fn main() { + let a = [1, 2, 3, 4, 5]; + + let element = a[10]; + + println!("The value of element is: {}", element); +} +``` + +Running this code with `cargo run` produces: + +```bash +$ cargo run + Compiling arrays v0.1.0 (file:///projects/arrays) + Running `target/debug/arrays` +thread '
' panicked at 'index out of bounds: the len is 5 but the index is 10', src/main.rs:4 +note: Run with `RUST_BACKTRACE=1` for a backtrace. +error: Process didn't exit successfully: `target/debug/arrays` (exit code: 101) +``` + +We can see that compiling did not give us any errors, but we got a *runtime* +error and our program didn't exit successfully. When we attempt to access an +element using indexing, Rust will check that the index we've specified is less +than the array length. If the index is greater than the length, it will +"panic", which is what it's called when a Rust program exits with an error. + +This is our first example of Rust’s safety principles in action. In many +low-level languages, this kind of check is not done, and when you provide an +incorrect index, invalid memory can be accessed. Rust protects us against this +kind of error by immediately exiting instead of allowing the memory access and +continuing. We'll discuss more of Rust’s error handling in Chapter XX. diff --git a/src/ch03-02-variable-bindings.md b/src/ch03-02-variable-bindings.md deleted file mode 100644 index 9e6f11b19..000000000 --- a/src/ch03-02-variable-bindings.md +++ /dev/null @@ -1,522 +0,0 @@ -# Variable Bindings - -The foundation of virtually every program is the ability to store and modify -data. Rust programs are no different. Let’s start with a short example. - -## The basics of bindings - -First, we’ll generate a new project with Cargo. Open a terminal, and navigate -to the directory where you’d like to keep your projects. From there, let’s -generate a new project: - -```bash -$ cargo new --bin bindings -$ cd bindings -``` - -This creates a new project, ‘bindings’, and sets up our `Cargo.toml` and -`src/main.rs` files. As we saw in “Hello, World!”, Cargo will generate these -files and create a little ‘hello world’ program for us: - -```rust -fn main() { - println!("Hello, world!"); -} -``` - -Let’s replace that program with this one: - -```rust -fn main() { - let x = 5; - - println!("The value of x is: {}", x); -} -``` - -And finally, run it: - -```bash -$ cargo run - Compiling bindings v0.1.0 (file:///projects/bindings) - Running `target/debug/bindings` -The value of x is: 5 -``` - -If you see an error instead, double check that you have copied the program -exactly as written. Let’s break this down, line by line. - -```rust,ignore -fn main() { -``` - -The `main()` function is the entry point of every Rust program. We’ll talk more -about functions in the next section, but for now, all we need to know is that -this is where our program begins. The opening curly brace, `{`, indicates the -start of the function’s body. - -```rust,ignore - let x = 5; -``` - -This is our first ‘variable binding’, which we create with a ‘`let` statement’. - -Before we do that, though, let’s finish investigating this example. Here’s the -next line: - -```rust,ignore - println!("The value of x is: {}", x); -``` - -The `println!` macro prints text to the screen. We can tell that it’s a macro -due to the `!`. We won’t learn how to write macros until much later in the -book, but we’ll use macros provided by the standard library throughout. Every -time you see a `!`, remember that it signifies a macro. Macros can add new -syntax to the language, and the `!` is a reminder that things may look slightly -unusual. - -`println!`, specifically, has one required argument, a ‘format string’, and -zero or more optional arguments. The format string can contain the special text -`{}`. Each instance of `{}` corresponds to an additional argument. Here’s an -example: - -```rust -let x = 2 + 3; -let y = x + 5; -println!("The value of x is {}, and the value of y is {}", x, y); -``` - -You can think of `{}` as little crab pincers, holding the value in place. This -placeholder has a number of more advanced formatting options that we’ll discuss -later. - -```rust,ignore -} -``` - -Finally, a closing curly brace matches up with the opening curly brace that -declared the `main()` function, and declares its end. - -This explains our output: - -```text -The value of x is: 5 -``` - -We assign `5` to a binding, `x`, and then print it to the screen with -`println!`. - - -## Delayed Initialization - -We do not have to provide bindings with an initial value, and can assign it -later. Try this program: - -```rust -fn main() { - let x; - - x = 5; - - println!("The value of x is: {}", x); -} -``` - -And run it with `cargo run`: - -```text -$ cargo run - Compiling bindings v0.1.0 (file:///projects/bindings) - Running `target/debug/bindings` -The value of x is: 5 -``` - -It’s all good. This raises a question, though: what if we try to print out a -binding before we declare a value? Here’s a program that demonstrates this -question: - -```rust,ignore -fn main() { - let x; - - println!("The value of x is: {}", x); - - x = 5; -} -``` - -We can find out the answer with `cargo run`: - -```text - Compiling bindings v0.1.0 (file:///projects/bindings) -src/main.rs:4:39: 4:40 error: use of possibly uninitialized variable: `x` [E0381] -src/main.rs:4 println!(“The value of x is: {}”, x); - ^ -:2:25: 2:56 note: in this expansion of format_args! -:3:1: 3:54 note: in this expansion of print! (defined in ) -src/main.rs:4:5: 4:42 note: in this expansion of println! (defined in ) -src/main.rs:4:39: 4:40 help: run `rustc --explain E0381` to see a detailed explanation -error: aborting due to previous error -Could not compile `bindings`. - -To learn more, run the command again with --verbose. -``` - -An error! The compiler won’t let us write a program like this. This is our -first example of the compiler helping us find an error in our program. -Different programming languages have different ways of approaching this -problem. Some languages always initialize values with some sort of default. -Other languages leave the value uninitialized, and make no promises about what -happens if you try to use something before initialization. Rust chooses -something else: error and force the programmer to explain what they want. We -must do some sort of initialization before we can use `x`. - -### Extended error explanations - -There’s one more interesting part of this error message: - -```text -src/main.rs:4:39: 4:40 help: run `rustc --explain E0381` to see a detailed explanation -``` - -We can see an extended explanation by passing the `--explain` flag to `rustc`. -Not every error has a longer explanation, but many of them do. These extended -explanations try to show off common ways that the error occurs, and common -solutions to the issue. Here’s `E0381`: - -```bash -$ rustc --explain E0381 -It is not allowed to use or capture an uninitialized variable. For example: - -fn main() { - let x: i32; - let y = x; // error, use of possibly uninitialized variable - -To fix this, ensure that any declared variables are initialized before being -used. -``` - -These explanations can really help if you’re stuck on an error. The compiler is -your friend, and is here to help. - -## Mutable bindings - -What about changing the value of a binding? Here’s another sample program that -asks this question: - -```rust,ignore -fn main() { - let x = 5; - - x = 6; - - println!("The value of x is: {}", x); -} -``` - -`cargo run` has the answer for us: - -```bash -$ cargo run - Compiling bindings v0.1.0 (file:///projects/bindings) -src/main.rs:4:5: 4:10 error: re-assignment of immutable variable `x` [E0384] -src/main.rs:4 x = 6; - ^~~~~ -src/main.rs:4:5: 4:10 help: run `rustc --explain E0384` to see a detailed explanation -src/main.rs:2:9: 2:10 note: prior assignment occurs here -src/main.rs:2 let x = 5; - ^ -``` - -The error mentions `re-assigment of immutable variable`. That’s right: bindings -are immutable. But they’re only immutable by default. In a pattern, when we’re -creating a new name, we can add `mut` in front to make the binding a mutable -one. Here’s an example: - -```rust -fn main() { - let mut x = 5; - - println!("The value of x is: {}", x); - - x = 6; - - println!("The value of x is: {}", x); -} -``` - -Running this, we get: - -```bash -$ cargo run - Compiling bindings v0.1.0 (file:///projects/bindings) - Running `target/debug/bindings` -The value of x is: 5 -The value of x is: 6 -``` - -We can now change the value that `x` binds to. Note that the syntax is not `let -mut` exactly; it’s using `mut` in a pattern. This becomes more obvious with our -`()` pattern: - - -```rust,ignore -fn main() { - let (mut x, y) = (5, 6); - - x = 7; - y = 8; -} -``` - -The compiler will complain about this program: - -```bash -$ cargo build - Compiling bindings v0.1.0 (file:///projects/bindings) -src/main.rs:5:5: 5:10 error: re-assignment of immutable variable `y` [E0384] -src/main.rs:5 y = 8; - ^~~~~ -src/main.rs:5:5: 5:10 help: run `rustc --explain E0384` to see a detailed explanation -src/main.rs:2:17: 2:18 note: prior assignment occurs here -src/main.rs:2 let (mut x, y) = (5, 6); - ^ -``` - -It’s fine with re-assigning `x`, but not `y`. The `mut` only applies to the -name that follows it, not the whole pattern. - -### Reassignment, not mutation - -There is one subtlety we haven’t covered yet: `mut` allows you to mutate _the -binding_, but not _what the binding binds to_. In other words: - -```rust -fn main() { - let mut x = 5; - - x = 6; -} -``` - -This is not changing the value that `x` is bound to, but creating a new value, -`6`, and changing the binding to bind to it instead. It’s a subtle but -important difference. Well, for now, it does not make a lot of difference, but -when our programs get more complex, it will. Specifically, passing arguments to -functions will illustrate the difference. We’ll talk about that in the next -section, when we discuss functions. - -## Scope - -Variable bindings have a ‘scope’ in which they’re valid. That scope begins from -the point at which the binding is declared, and ends at the end of the next -block of code. We can only access bindings which are ‘in scope’. We cannot -access them ‘before they come into scope’ or ‘after they go out of scope’. -Here’s an example: - -```rust -fn main() { - println!("x is not yet in scope"); - - let x = 5; - println!("x is now in scope"); - - println!("In real code, we’d now do a bunch of work."); - - println!("x will go out of scope now! The next curly brace is ending the main function."); -} -``` - -We can create arbitrary scopes through the use of `{` and `}`: - -```rust -fn main() { - println!("x is not yet in scope"); - - let x = 5; - println!("x is now in scope"); - - println!("Let’s start a new scope!"); - - { - let y = 5; - println!("y is now in scope"); - println!("x is also still in scope"); - - println!("y will go out of scope now!"); - println!("The next curly brace is ending the scope we started."); - } - - println!("x is still in scope, but y is now out of scope and is not usable"); - - println!("x will go out of scope now! The next curly brace is ending the main function."); -} -``` - -What bindings are in and out of scope will become much more important later, -once we learn about ‘references’ and ‘traits’. - -## Shadowing - -A final thing about bindings: they can ‘shadow’ previous bindings with the same -name. Here’s a sample program: - -```rust -fn main() { - let x = 5; - let x = 6; - - println!("The value of x is: {}", x); -} -``` - -Running it, we can see the shadowing in action: - -```text -src/main.rs:2:9: 2:10 warning: unused variable: `x`, #[warn(unused_variables)] on by default -src/main.rs:2 let x = 5; - ^ - Running `target/debug/bindings` -The value of x is: 6 -``` - -There are two interesting things in this output. First, Rust will compile and -run this program, no problem. And as we can see, the value of `x` is `6`. But -we didn’t declare `x` as mutable. Instead, we declared a _new_ binding that is -_also_ named `x`, and gave it a new value. The older value that we bound `x` to -is inaccessible as soon as the new `x` is declared. This can be useful if you’d -like to perform a few transformations on a value, and leave it immutable. For -example: - -```rust -fn main() { - let x = 5; - let x = x + 1; - let x = x * 2; - - println!("The value of x is: {}", x); -} -``` - -This will print: - -```bash - Compiling bindings v0.1.0 (file:///projects/bindings) - Running `target/debug/bindings` -The value of x is: 12 -``` - -This lets us modify `x`, but not deal with mutation. This is nice because we -know that the compiler will let us know if we try to modify it later. Let’s -assume that after we calculate `12`, we don’t want to modify `x` again. If we -had written this program in a mutable style, like this: - -```rust -fn main() { - let mut x = 5; - x = x + 1; - x = x * 2; - - println!("The value of x is: {}", x); - - x = 15; - - println!("The value of x is: {}", x); -} -``` - -Rust is happy to let us mutate it again, to `15`. A similar program in our -immutable style will let us know about that accidental mutation, however: - -```rust,ignore -fn main() { - let x = 5; - let x = x + 1; - let x = x * 2; - - println!("The value of x is: {}", x); - - x = 15; - - println!("The value of x is: {}", x); -} -``` - -If we try to compile, we get an error: - -```bash -$ cargo build - Compiling bindings v0.1.0 (file:///projects/bindings) -src/main.rs:8:5: 8:11 error: re-assignment of immutable variable `x` [E0384] -src/main.rs:8 x = 15; - ^~~~~~ -src/main.rs:8:5: 8:11 help: run `rustc --explain E0384` to see a detailed explanation -src/main.rs:4:9: 4:10 note: prior assignment occurs here -src/main.rs:4 let x = x * 2; - ^ -error: aborting due to previous error -Could not compile `bindings`. -``` - -Exactly what we wanted. - -Shadowing can take some time to get used to, but it’s very powerful, and works -well with immutability. - -There was one more thing we should talk about in the output from compiling our -initial program. It’s this part: - -```text -src/main.rs:2:9: 2:10 warning: unused variable: `x`, #[warn(unused_variables)] on by default -``` - -Here’s the two lines of relevant code: - -```rust -let x = 5; -let x = 6; -``` - -Rust knows that we shadowed `x`, but we never ended up using the initial value. -This isn’t _wrong_, exactly, it just may not have been what we wanted. In this -case, the compiler issues a ‘warning’, but still compiles our program. The -`#[warn(unused_variables)]` syntax is called an ‘attribute’, which we’ll -discuss in a later section. More specifically, a warning like this is called a -‘lint’, which is an old term for the bits of sheep’s wool that you wouldn’t -want to put in cloth. Similarly, this lint is telling us that we may have an -extra bit of code we don’t need. Our program would work just fine without it. -It’s worth listening to these warnings, and fixing the problems they point out. -They can be signs of a larger problem. In this case, we may not have realized -that we were shadowing `x`. - -### Shadowing and scopes - -Like any binding, a binding that shadows another binding will go away at the -end of a scope. Here’s an example program: - -```rust -fn main() { - let x = 5; - - println!("Before shadowing, x is: {}", x); - - { - let x = 6; - - println!("Now that x is shadowed, x is: {}", x); - } - - println!("After shadowing, x is: {}", x); -} -``` - -If we run this example, we can see the shadow appear and disappear: - -```bash -$ cargo run - Compiling bindings v0.1.0 (file:///projects/bindings) - Running `target/debug/bindings` -Before shadowing, x is: 5 -Now that x is shadowed, x is: 6 -After shadowing, x is: 5 -``` diff --git a/src/ch03-03-functions.md b/src/ch03-03-functions.md deleted file mode 100644 index 6992cc41a..000000000 --- a/src/ch03-03-functions.md +++ /dev/null @@ -1,504 +0,0 @@ -# Functions - -Functions are pervasive in Rust code. We’ve already seen the most important -function, `main()`, in previous sections of the book: - -```rust -fn main() { - println!("Hello, world!"); -} -``` - -We can declare new functions with the `fn` keyword: - -```rust -fn another_function() { - println!("Another function."); -} -``` - -Rust code uses `snake_case` as a style for function names: all lower case, with -underscores separating words. (It also uses them for variable names, too.) We -can call any function we’ve defined by using its name and some parentheses: - -```rust -fn main() { - println!("Hello, world!"); - - another_function(); -} - -fn another_function() { - println!("Another function."); -} -``` - -Let’s start a new project to explore functions. Open a terminal, and navigate -to the directory where you’d like to keep your projects. From there, use Cargo -to generate a new project: - -```bash -$ cargo new --bin functions -$ cd functions -``` - -Place the new example in `src/main.rs`, and run it: - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) - Running `target/debug/functions` -Hello, world! -Another function. -``` - -As we can see, the lines execute in order: first, we print out our “Hello, -world!” message, and then `another_function()` is called. It then prints its -message as well. - -## Function Arguments - -Functions can also take arguments: - -```rust -fn main() { - another_function(5); -} - -fn another_function(x: i32) { - println!("The value of x is: {}", x); -} -``` - -Let’s try running it: - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) - Running `target/debug/functions` -The value of x is: 5 -``` - -Let’s take a closer look at `another_function()`’s signature: - -```rust,ignore -fn another_function(x: i32) { -``` - -Declaring a function which takes a single argument looks like this: - -```text -fn NAME(PATTERN: TYPE) { -``` - -That’s right, patterns appear again. Consider how the parameter declaration -here looks like the `let` bindings we used earlier: - -```rust,ignore -let x: i32; -fn another_function(x: i32) { -``` - -There’s only one difference here: in function signatures, we _must_ declare the -type. This is a deliberate decision; we find that requiring type annotations in -functions means that you almost never need them anywhere else. - -You can separate multiple arguments with a comma: - -```text -fn NAME(PATTERN: TYPE, PATTERN: TYPE, PATTERN: TYPE, PATTERN: TYPE...) { -``` - -Here’s a full example: - -```rust -fn main() { - another_function(5, 6); -} - -fn another_function(x: i32, y: i32) { - println!("The value of x is: {}", x); - println!("The value of y is: {}", y); -} -``` - -Let’s try it: - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) - Running `target/debug/functions` -The value of x is: 5 -The value of y is: 6 -``` - -We could also create bindings, and pass them in as arguments: - -```rust -fn main() { - let a = 5; - let b = 6; - - another_function(a, b); -} - -fn another_function(x: i32, y: i32) { - println!("The value of x is: {}", x); - println!("The value of y is: {}", y); -} -``` - -This has the same effect: - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) - Running `target/debug/functions` -The value of x is: 5 -The value of y is: 6 -``` - -Note that our bindings are called `a` and `b`, yet inside of the function, we -refer to them by the names in the signature, `x` and `y`. Inside a function, -only its parameters are in scope, so we need to use those names. Bindings -passed as parameters don’t need to have the same name as the arguments. - -## Return values - -Functions can also return values back to the function that called them: - -```TEXT -fn NAME(PATTERN: TYPE, PATTERN: TYPE, PATTERN: TYPE, PATTERN: TYPE...) -> TYPE { -``` - -We don’t name return values, but we do declare their type, after an arrow: -`->`. Here’s a sample program: - -```rust -fn main() { - let x = five(); - - println!("The value of x is: {}", x); -} - -fn five() -> i32 { - 5 -} -``` - -Let’s try running it: - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) - Running `target/debug/functions` -The value of x is: 5 -``` - -Let’s examine this in more detail. There are two important bits. First, we can -use the return value of a function to initialize a binding: - -```rust,ignore -let x = five(); -``` - -Because `five()` returns a `5`, this is the same as: - -```rust -let x = 5; -``` - -The second interesting bit is `five()` itself: - -```rust -fn five() -> i32 { - 5 -} -``` - -We have no arguments, and our return type, `i32`. However, the body of this -function is a lonely `5`. There’s a detail here that you may or may not have -noticed: we’ve ended almost every line in our programs with a semicolon. -There’s no semicolon here, though. Why not? - -The answer to this question is: - -> The return value of a function is the value of its final expression. - -We haven’t talked about expressions yet, so this definition doesn’t help a lot. -Let’s go over that now. - -## Statements and Expressions - -Expressions are bits of code that evaluate to a value. Consider some math -operations, like this: - -```rust,ignore -5 + 6 -``` - -We can evaluate this expression, and come up with a value: `11`. In Rust, most -bits of code are expressions. For example, calling a function is an expression: - -```rust,ignore -foo(5) -``` - -The value is equal to whatever the return value of `foo()` is. - -So why does this matter? Well, not everything is an expression. Some things are -‘statements’. Expressions _compute_ something, but statements _bind_ or _do_ -something. It’s a subtle difference. We’ve already seen two kinds of -statements: `let` statements, and `fn` declarations. - -Because `let` is a statement, not an expression, you can’t assign it to another -binding. Here’s an example that doesn’t work: - -```rust,ignore -fn main() { - let x = (let y = 6); -} -``` - -If we try to run this program, we’ll get an error: - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) -src/main.rs:2:14: 2:17 error: expected identifier, found keyword `let` -src/main.rs:2 let x = (let y = 6); - ^~~ -src/main.rs:2:18: 2:19 error: expected one of `!`, `)`, `,`, `.`, `::`, `{`, or an operator, found `y` -src/main.rs:2 let x = (let y = 6); - ^ -Could not compile `functions`. -``` - -We also cannot somehow assign a `fn` declaration to a binding, either. - -So what’s this have to do with return values? Well, `{}`, a ‘block’ that we -used earlier to create new scopes, _is_ an expression. Let’s take a closer look -at `{}`. It looks like this: - -```text -{ - STATEMENT* - EXPRESSION -} -``` - -The `*` there means ‘zero or more’, so we can have any number of statements -followed by an expression. Since blocks are expressions themselves, we can nest -blocks inside of blocks. And since they return a value, we can use them in -`let` statements: - -```rust -fn main() { - let x = 5; - - let y = { - let z = 1; - - x + z + 5 - }; - - println!("The value of y is: {}", y); -} -``` - -Let’s try running this program: - -```bash - Compiling functions v0.1.0 (file:///projects/functions) - Running `target/debug/functions` -The value of y is: 11 -``` - -We’re now using a block to give us a value for `y`: - -```rust,ignore -let y = { - -}; -``` - -Since the block can contain statements, we create a new variable binding, `z`, -and give it a value. We then do some math for the final expression of the -block: - -```rust,ignore -{ - let z = 1; - - x + z + 5 -} -``` - -`5 + 1 + 5` is `11`, and so the value of the entire block is `11`. This gets -substituted into our `let` statement for `y`: - -```rust,ignore -let y = 11; -``` - -Hence our output saying `y` is `11`. - -Where else do we use blocks? As the body of functions! They’re very similar: - -```rust -fn main() { - let x = 5; - - let y = { - x + 1 - }; - - println!("The value of y is: {}", y); - - let y = plus_one(x); - - println!("The value of y is: {}", y); -} - -fn plus_one(x: i32) -> i32 { - x + 1 -} -``` - -Running this gives: - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) - Running `target/debug/functions` -The value of y is: 6 -The value of y is: 6 -``` - -In both cases, we use a block to produce a value. In the first case, it’s -assigning with `let`: - -```rust,ignore -let y = { -``` - -In the second, it’s the return value of the function: - -```rust,ignore -fn plus_one(x: i32) -> i32 { -``` - -### Expression statements - -There’s one more detail about expressions and statements: a semicolon takes any -expression, and turns it into a statement. Let’s accidentally cause an error -with `plus_one()`: - -```rust,ignore -fn main() { - let x = plus_one(5); - - println!("The value of x is: {}", x); -} - -fn plus_one(x: i32) -> i32 { - x + 1; -} -``` - -Instead of an expression, `x + 1`, we’ve now turned it into a statement, -`x + 1;`. - -Running this gives an error: - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) -src/main.rs:7:1: 9:2 error: not all control paths return a value [E0269] -src/main.rs:7 fn plus_one(x: i32) -> i32 { -src/main.rs:8 x + 1; -src/main.rs:9 } -src/main.rs:7:1: 9:2 help: run `rustc --explain E0269` to see a detailed explanation -src/main.rs:8:10: 8:11 help: consider removing this semicolon: -src/main.rs:8 x + 1; - ^ -error: aborting due to previous error -Could not compile `functions`. -``` - -Rust has our back here: it even suggests removing the semicolon, which fixes -the error. But the main error message is the core of the issue: statements -don’t evaluate to a value, yet we want to return an `i32`. - -In practice, Rust programmers don’t often think about these rules at this -level. Usually, you have a semicolon at the end of most lines, and maybe not at -the end of blocks. - -## Multiple return values - -Functions cannot directly return multiple values. There’s a trick, however. -Remember the `()`s we used when showing off complex bindings? - -```rust -fn main() { - let (x, y) = (5, 6); -} -``` - -They form something called a ‘tuple’, one of Rust’s basic types. A tuple is an -anonymous collection of elements. But since a tuple is a singular thing, we can -use it as a way to return multiple values from functions: - -```rust -fn main() { - let (x, y) = two_numbers(); - - println!("The value of x is: {}", x); - println!("The value of y is: {}", y); -} - -fn two_numbers() -> (i32, i32) { - (5, 6) -} -``` - -Running this will show us the values: - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) - Running `target/debug/functions` -The value of x is: 5 -The value of y is: 6 -``` - -There are two interesting changes here: assigning the return value of -`two_numbers()` to `x` and `y`, and the declaration of `two_numbers()` itself. - -Let’s look at the declaration first: - -```rust -fn two_numbers() -> (i32, i32) { - (5, 6) -} -``` - -The `(i32, i32)` should look familiar. We saw it in `let` bindings earlier: - -```rust -let (x, y): (i32, i32) = (5, 6); -``` - -The `(i32, i32)` syntax says “a tuple with two `i32`s in it.” The `(5, 6)` -syntax creates a new one, with `5` and `6`. - -This tuple is then returned, and assigned to `x` and `y`: - -```rust,ignore -let (x, y) = two_numbers(); -``` - -See how all these bits fit together? - -We call this behavior of `let` ‘destructuring’, because it takes the structure -of the expression that comes after the `=` and takes it apart. diff --git a/src/ch03-03-how-functions-work.md b/src/ch03-03-how-functions-work.md new file mode 100644 index 000000000..b5f1e6805 --- /dev/null +++ b/src/ch03-03-how-functions-work.md @@ -0,0 +1,330 @@ +## How Functions Work + +Functions are pervasive in Rust code. We’ve already seen one of the most +important functions in the language: the `main()` function that’s the entry +point of many programs. We've also seen the `fn` keyword, which allows us to +declare new functions. + +Rust code uses *snake case* as the conventional style for function names. In +snake case, all letters are lower case, and there are underscores separating +words. (Rust also uses snake case for the names of variable bindings; we just +haven't used any variable bindings with enough letters to need underscores +yet). Here's a program containing an example function definition: + +```rust +fn main() { + println!("Hello, world!"); + + another_function(); +} + +fn another_function() { + println!("Another function."); +} +``` + +Function definitions in Rust start with `fn` and have a set of parentheses +after the function name. The curly braces tell the compiler where the function +body begins and ends. + +We can call any function we’ve defined by entering its name followed by a pair +of parentheses. Since `another_function()` is defined in the program, it can be +called from inside the `main()` function. Note that we defined +`another_function()` _after_ the `main()` function in our source code; we could +have defined it before as well. Rust doesn’t care where you define your +functions, only that they are defined somewhere. + +Let’s start a new project to explore functions further. Open a terminal, and +navigate to the directory you're keeping your projects in. From there, use +Cargo to generate a new project, as follows: + +```bash +$ cargo new --bin functions +$ cd functions +``` + +Place the `another_function()` example in a file named *src/main.rs* and run +it. You should see the following output: + +```bash +$ cargo run + Compiling functions v0.1.0 (file:///projects/functions) + Running `target/debug/functions` +Hello, world! +Another function. +``` + +The lines execute in the order they appear in the `main()` function. First, our +“Hello, world!” message prints, and then `another_function()` is called and its +message is printed. + +### Function Arguments + +Functions can also take arguments. The following rewritten version of +`another_function()` shows what arguments look like in Rust: + +```rust +fn main() { + another_function(5); +} + +fn another_function(x: i32) { + println!("The value of x is: {}", x); +} +``` + +Try running this program, and you should get this output: + +```bash +$ cargo run + Compiling functions v0.1.0 (file:///projects/functions) + Running `target/debug/functions` +The value of x is: 5 +``` + +Since we passed `5` to `another_function()`, the `println!` macro put `5` where +the pair of curly braces were in the format string. The declaration of +`another_function()` shows that it takes one argument named `x`, and the type +of `x` is `i32`. + +In function signatures, we _must_ declare the type. This is a deliberate +decision in the design of Rust; requiring type annotations in function +definitions means the compiler almost never needs you to use them elsewhere in +the code in order to figure out what you mean. + +When you want a function to have multiple arguments, just separate them inside +the function signature with commas, like this: + +```rust +fn main() { + another_function(5, 6); +} + +fn another_function(x: i32, y: i32) { + println!("The value of x is: {}", x); + println!("The value of y is: {}", y); +} +``` + +In this example, we make a function with two arguments, both of which are +`i32`s. If your function has multiple arguments, they don’t need to be the same +type, but they just happen to be in this example. Our function then prints out +the values of both of its arguments. + +Let’s try out this code. Replace the program currently in your `function` +project's `main.rs` file with the example above, and run it as follows: + +```bash +$ cargo run + Compiling functions v0.1.0 (file:///projects/functions) + Running `target/debug/functions` +The value of x is: 5 +The value of y is: 6 +``` + +Since `5` is passed as the `x` argument and `6` is passed as the `y` argument, +the two strings are printed with these values. + +### Function Bodies + +Function bodies are made up of a series of statements ending in an optional +expression. So far, we've only seen functions without an ending expression, but +we have seen expressions as parts of statements. Since Rust is an +expression-based language, this is an important distinction to understand. +Other languages don't have the same distinctions, so let's look at what +statements and expressions are and how their differences affect the bodies of +functions. + +#### Statements and Expressions + +We've already been using both statements and expressions. *Statements* are +instructions that perform some action and do not return a value. *Expressions* +evaluate to a resulting value. Let's look at some examples. + +`Let` bindings are statements. They instruct the program to create a binding +name and assign a value to it. `let y = 6;` in this example is a statement: + +```rust +fn main() { + let y = 6; +} +``` + +Function definitions are also statements-- so the entire previous example is a +statement as well. + +Statements do not return values themselves. Therefore, you can’t assign a `let` +binding to another binding, as this code tries to do: + +```rust,ignore +fn main() { + let x = (let y = 6); +} +``` + +If we were to run this program, we’d get an error like this: + +```bash +$ cargo run + Compiling functions v0.1.0 (file:///projects/functions) +error: expected expression, found statement (`let`) + --> src/main.rs:2:14 +2 |> let x = (let y = 6); + |> ^^^ +note: variable declaration using `let` is a statement + +error: aborting due to previous error +error: Could not compile `functions`. +``` + +The `let y = 6` statement does not return a value, so there isn't anything for +`x` to bind to. This is different than in other languages like C and Ruby where +the assignment returns the value of the assignment. In those languages, you +could write `x = y = 6` and have both `x` and `y` have the value `6`, but that +is not the case in Rust. + +Expressions are most of the rest of the code that you will write in Rust. +Consider a simple math operation, like this: + +```rust,ignore +5 + 6 +``` + +This is an expression, and evaluating it results in the value `11`. Expressions +can be part of statements-- in the previous example that had the statement `let +y = 6;`, `6` is an expression that evaluates to the value `6`. Calling a +function is an expression. Calling a macro is an expression. The block that we +use to create new scopes, `{}`, is an expression, for example: + +```rust +fn main() { + let x = 5; + + let y = { + let x = 3; + x + 1 + }; + + println!("The value of y is: {}", y); +} +``` + +The expression: + +```rust,ignore +{ + let x = 3; + x + 1 +} +``` + +is a block that, in this case, gets evaluated to `4`, which then gets bound to +`y` as part of the `let` statement. + +Note that the line containing `x + 1` does not have a semicolon at the end like +most of the lines we've seen up until now have had. This is the most important +distinction between expressions and statements to remember: statements end in +semicolons while expressions do not. If you add a semicolon to the end of an +expression, that will turn it into a statement, which will then not return a +value. Keep this in mind as we explore function return values and expressions. + +### Functions with Return Values + +Functions can return values back to the code that calls them. We don’t name +return values, but we do declare their type, after an arrow (`->`). In Rust, +the "return value of the function” is synonymous with “the value of the final +expression in the block of the body of a function.” Here's an example of a +function that returns a value: + +```rust +fn five() -> i32 { + 5 +} + +fn main() { + let x = five(); + + println!("The value of x is: {}", x); +} +``` + +There are no function calls, macros, or even `let` statements in the `five()` +function-- just the number `5` by itself. That's a perfectly valid function in +Rust. Note the function's return type, too. Try running this code, and the +output should look like this: + +```bash +$ cargo run + Compiling functions v0.1.0 (file:///projects/functions) + Running `target/debug/functions` +The value of x is: 5 +``` + +The `5` in `five()` is actually the function's return value, which is why the +return type is `i32`. Let’s examine this in more detail. There are two +important bits. First, the line `let x = five();` in `main()` shows that we can +use the return value of a function to initialize a binding. + +Because the function `five()` returns a `5`, that line is the same as saying: + +```rust +let x = 5; +``` + +The second interesting bit is the `five()` function itself. It requires no +arguments and defines the type of the return value, but the body of the +function is a lonely `5` with no semicolon because it is an expression whose +value we want to return. Let's look at another example: + +```rust +fn main() { + let x = plus_one(5); + + println!("The value of x is: {}", x); +} + +fn plus_one(x: i32) -> i32 { + x + 1 +} +``` + +Running this code will print `The value of x is: 6`. What happens if we put a +semicolon at the end of the line containing `x + 1`, changing it from an +expression to a statement? + +```rust,ignore +fn main() { + let x = plus_one(5); + + println!("The value of x is: {}", x); +} + +fn plus_one(x: i32) -> i32 { + x + 1; +} +``` + +Running this code gives an error, as follows: + +```bash +$ cargo run + Compiling functions v0.1.0 (file:///projects/functions) +error: not all control paths return a value [--explain E0269] + --> src/main.rs:7:1 +7 |> fn plus_one(x: i32) -> i32 { + |> ^ +help: consider removing this semicolon: + --> src/main.rs:8:10 +8 |> x + 1; + |> ^ + +error: aborting due to previous error +error: Could not compile `functions`. +``` + +The main error message, "not all control paths return a value", reveals the +core of the issue with this code. The definition of the function `plus_one` +says that it will return an `i32`, but statements don’t evaluate to a value. +Therefore, nothing is returned, which contradicts the function definition and +results in an error. In this output, Rust gives an option to rectify this: it +suggests removing the semicolon, which would fix the error. diff --git a/src/ch03-04-comments.md b/src/ch03-04-comments.md new file mode 100644 index 000000000..a4c950b9e --- /dev/null +++ b/src/ch03-04-comments.md @@ -0,0 +1,41 @@ +## Comments + +All programmers strive to make their code easy to understand, but sometimes +extra explanation is warranted. In these cases, we leave notes in our source +code that the compiler will ignore but people reading the source code may find +useful. These notes are called *comments*. + +Here’s a simple comment: + +```rust +// Hello, world. +``` + +In Rust, comments must start with two slashes and will last until the end of +the line. For comments that extend beyond a single line, you'll need to +include `//` on each line, like this: + +```rust +// So we’re doing something complicated here, long enough that we need +// multiple lines of comments to do it! Whew! Hopefully, this comment will +// explain what’s going on. +``` + +Comments can also be placed at the end of lines of code: + +```rust +fn main() { + let lucky_number = 7; // I’m feeling lucky today. +} +``` + +But you’ll more often see them above, like so: + +```rust +fn main() { + // I’m feeling lucky today. + let lucky_number = 7; +} +``` + +That’s all there is to it. Comments are not particularly complicated. diff --git a/src/ch03-04-scalar-types.md b/src/ch03-04-scalar-types.md deleted file mode 100644 index 93214d006..000000000 --- a/src/ch03-04-scalar-types.md +++ /dev/null @@ -1,116 +0,0 @@ -# Scalar Types - -We’ve seen that every value in Rust has a type of some kind. There are a number -of types which are built into the language itself. First, we’ll take a look at -‘scalar’ types, that is, types which represent a single value. - -Remember, you can rely on type inference to figure out the type of a binding, -or you can annotate it explicitly: - -```rust -fn main() { - let x: i32 = 5; -} -``` - -## Integers - -You’ve already seen one primitive type: `i32`. There are a number of built-in -number types in Rust. - -Here’s a chart of Rust’s integer types: - -| | signed | unsigned | -|--------|--------|----------| -| 8-bit | i8 | u8 | -| 16-bit | i16 | u16 | -| 32-bit | i32 | u32 | -| 64-bit | i64 | u64 | -| arch | isize | usize | - -We have both signed and unsigned variants of numbers, and each variant has an -explicit size. Unsigned numbers are never negative, and signed numbers can be -positive or negative. (Think ‘plus sign’ or ‘minus sign’: that’s a signed -number.) Signed numbers are stored using ‘two’s complement’ representation. - -Finally, `isize` and `usize` are different sizes based on the kind of computer -your program is running on. If you are on a 64-bit architecture, they are 64 -bits, and if you’re on a 32-bit one, they’re 32 bits. - -So how do you choose from all these options? Well, if you really don’t know, -the defaults are a good choice: integer types default to `i32`. The primary use -case for `isize`/`usize` is when indexing some sort of collection. We’ll talk -more about our first collection, arrays, in just a moment. - -## Floating-point numbers - -Rust also has two primitive floating-point numbers: `f32` and `f64`. They are -32 bits and 64 bits in size, respectively. The default is `f64`. - -```rust -fn main() { - let x = 2.0; // f64 - - let y: f32 = 3.0; // f32 -} -``` - -Floating-point numbers are represented according to the IEEE-754 standard. -`f32` is a single-precision float, `f64` is double-precision. - -## Numeric operations - -Rust supports the usual operations you’d expect on all of these number types: - -```rust -fn main() { - // addition - let sum = 5 + 10; - - // subtraction - let difference = 95.5 - 4.3; - - // multiplication - let product = 4 * 30; - - // division - let quotient = 56.7 / 32.2; - - // modulus - let remainder = 43 % 5; -} -``` - -## Booleans - -Somewhat fundamental to all computing, Rust has a boolean type, `bool`, with -two possible values: - -```rust -fn main() { - let t = true; - let f: bool = false; // with explicit type annotation -} -``` - -The main way to consume boolean values is through conditionals like `if`, which -we’ll see later in the chapter. - -## Characters - -We’ve only worked with numbers so far, but what about letters? Rust’s most -primitive alphabetic type is the `char`: - -```rust -fn main() { - let c = 'z'; - let z = 'ℤ'; -} -``` - -Rust’s `char` represents a [Unicode Scalar Value], which means that it can -represent a lot more than just ASCII. ‘Character’ isn’t really a concept in -Unicode, however: your human intuition for what a ‘character’ is may not match -up with a `char`. It also means that `char`s are four bytes each. - -[Unicode Scalar Value]: http://www.unicode.org/glossary/#unicode_scalar_value diff --git a/src/ch03-05-compound-types.md b/src/ch03-05-compound-types.md deleted file mode 100644 index 286547d76..000000000 --- a/src/ch03-05-compound-types.md +++ /dev/null @@ -1,234 +0,0 @@ -# Compound Types - -Now that we’ve discussed scalar types, let’s talk about compound types. -These types can group multiple values of scalar types into another type. - -## Tuples - -We’ve seen tuples before, in the guise of binding or returning multiple values -at once. It turns out that there’s no magic here: tuples are a general way of -making a compound value that groups some number of other values with distinct -types. The number of values grouped is the ‘arity’ of the tuple. - -We create a tuple by writing a comma-separated list of values inside -parentheses; each position in the tuple has a distinct type: - -```rust -fn main() { - let tup: (i32, f64, u8) = (500, 6.4, 1); -} -``` - -Note that, unlike the examples of multiple bindings, here we bound the -single name `tup` to the entire tuple. We can then use pattern -matching to destructure this tuple value: - -```rust -fn main() { - let tup: (i32, f64, u8) = (500, 6.4, 1); - let (x, y, z) = tup; - - println!("The value of y is: {}", y); -} -``` - -Tuples are used sparingly in Rust code. This is because the elements of a tuple -are anonymous, which can make code hard to read. - -### Tuple indexing - -In addition to destructuring through pattern matching, we can also access a -tuple element directly using `.`, followed by the index we want to access: - -```rust -fn main() { - let x: (i32, f64, u8) = (500, 6.4, 1); - - let five_hundred = x.0; - let six_point_four = x.1; - let one = x.2; -} -``` - -As you can see, the first index is `0`. - -### Single-element tuples - -There’s one last trick with tuples: `(5)` is actually ambiguous: is it a tuple, -or is it a `5` in parenthesis? If you need to disambiguate, use a comma: - -```rust -fn main() { - let x = (5); // x is an i32, no tuple. Think of it like (5 + 1) without the + 1, they’re for grouping. - - let x = (5,); // x is a (i32), a tuple with one element. -} -``` - -## Arrays - -So far, we’ve only represented single values in a binding. Sometimes, though, -it’s useful to have more than one value. These kinds of data structures are -called ‘collections’, and arrays are the ones we’ll learn about first. Arrays -look like this: - -```rust -fn main() { - let a = [1, 2, 3, 4, 5]; -} -``` - -An array’s type consists of the type of the elements it contains, as well as -the length: - -```rust -fn main() { - let a: [i32; 5] = [1, 2, 3, 4, 5]; -} -``` - -An array is a single chunk of memory, allocated on the stack. - -We can access elements of an array using indexing: - -```rust -fn main() { - let a = [1, 2, 3, 4, 5]; - - let first = a[0]; - let second = a[1]; -} -``` - -In this example, `first` will hold the value `1`, and `second` will be bound to -`2`. Note that these values are copied out of the array; if the array changes, -these bindings will not. Here’s an example, which also shows us how we can -modify elements of the array: - -```rust -fn main() { - let mut a = [1, 2, 3, 4, 5]; - - let first = a[0]; - - a[0] = 7; - - println!("The value of first is: {}", first); -} -``` - -Running this example will show that `first` is still `1`. If we didn’t want a -copy, but instead wanted to refer to the first element, whatever its value was, -we need a new concept. We’ll talk about ‘references’ in Section 4. - -One last thing: now that we are modifying the array, `a` needs to be declared -`mut`. - -Arrays are our first real data structure, and so there’s a few other concepts -that we haven’t covered in full yet. There are two: the `panic!` macro, and a -new way of printing things: `Debug`. - -### Panic - -We showed what happens when you access elements of an array, but what if we -give an invalid index? - -```rust,should_panic -fn main() { - let a = [1, 2, 3, 4, 5]; - - let invalid = a[10]; - - println!("The value of invalid is: {}", invalid); -} -``` - -If we run this example, we will get an error. Let’s re-use our `functions` -project from before. Change your `src/main.rs` to look like the example, and -run it: - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) - Running `target/debug/functions` -thread ‘
’ panicked at ‘index out of bounds: the len is 5 but the index is 10’, src/main.rs:4 -Process didn’t exit successfully: `target/debug/functions` (exit code: 101) -``` - -It says that our thread panicked, and that our program didn’t exit -successfully. There’s also a reason: we had a length of five, but an index of -10. - -For now, all you need to know is that a panic will crash your program. Rust’s -error handling story is described in full in a later chapter. - -So why did this code panic? Well, arrays know how many elements they hold. When -we access an element via indexing, Rust will check that the index is less than -the length. If it’s greater, it will panic, as something is very wrong. This is -our first example of Rust’s safety principles in action. In many low-level -languages, this kind of check is not done. If you have an incorrect index, -invalid memory can be accessed. Rust protects us against this kind of error. - -### Debug - -So far, we’ve been printing values using `{}`. If we try that with an array, -though... - -```rust,ignore -fn main() { - let a = [1, 2, 3, 4, 5]; - - println!("a is: {}", a); -} -``` - -... we will get an error: - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) -src/main.rs:4:25: 4:26 error: the trait `core::fmt::Display` is not implemented for the type `[_; 5]` [E0277] -src/main.rs:4 println!(“a is {}”, a); - ^ -:2:25: 2:56 note: in this expansion of format_args! -:3:1: 3:54 note: in this expansion of print! (defined in ) -src/main.rs:4:5: 4:28 note: in this expansion of println! (defined in ) -src/main.rs:4:25: 4:26 help: run `rustc --explain E0277` to see a detailed explanation -src/main.rs:4:25: 4:26 note: `[_; 5]` cannot be formatted with the default formatter; try using `:?` instead if you are using a format string -src/main.rs:4:25: 4:26 note: required by `core::fmt::Display::fmt` -error: aborting due to previous error -``` - -Whew! The core of the error is this part: the trait `core::fmt::Display` is not -implemented. We haven’t discussed traits yet, so this is bound to be confusing! -Here’s all we need to know for now: `println!` can do many kinds of formatting. -By default, `{}` implements a kind of formatting known as `Display`: output -intended for direct end-user consumption. The primitive types we’ve seen so far -implement `Display`, as there’s only one way you’d show a `1` to a user. But -with arrays, the output is less clear. Do you want commas or not? What about -the `[]`s? - -Due to these questions, more complex types in the standard library do not -implement `Display` formatting. There is another kind of formatting, `Debug`, -which is a bit different: intended for programmer consumption. We can ask -`println!` to use `Debug` formatting with `:?`: - -```rust -fn main() { - let a = [1, 2, 3, 4, 5]; - - println!("a is {:?}", a); -} -``` - -This will work: - -```bash -$ cargo run - Compiling functions v0.1.0 (file:///projects/functions) - Running `target/debug/functions` -a is [1, 2, 3, 4, 5] -``` - -You’ll see this repeated later, with other types. And we’ll cover traits fully -later in the book, Section 9. diff --git a/src/ch03-05-control-flow.md b/src/ch03-05-control-flow.md new file mode 100644 index 000000000..500050ca1 --- /dev/null +++ b/src/ch03-05-control-flow.md @@ -0,0 +1,408 @@ +## Control Flow + +Deciding whether or not to run some code depending on if a condition is true, +or deciding to run some code repeatedly while a condition is true, are basic +building blocks in most programming languages. The most common constructs that +let us control the flow of execution of our Rust code are `if` expressions and +loops. + +### `if` Expressions + +An `if` expression allows us to branch our code depending on conditions. We +provide a condition and then say, "If this condition is met, then run this +block of code. If the condition is not met, do not run this block of code." + +Let’s make a new project to explore `if`. Navigate to your projects directory, +and use Cargo to make a new project called `branches`: + +```bash +$ cargo new --bin branches +$ cd branches +``` + +Write this sample program using `if` and save it in the *branches* directory in +`src/main.rs`: + +```rust +fn main() { + let number = 3; + + if number < 5 { + println!("condition was true"); + } else { + println!("condition was false"); + } +} +``` + +All `if` expressions start with `if`, which is followed by a condition. In this +case, our condition is checking if our variable binding `number` has a value +that is less than 5. The block of code we want to execute if the condition is +true goes immediately after the condition, inside curly braces. These blocks +are sometimes called ‘arms’. We can optionally also include an `else` +statement, which we have chosen to do here. `else` gives the program a block of +code to execute should `condition` evaluate to false. + +Try running this code, and you should see output like this: + +```bash +$ cargo run + Compiling branches v0.1.0 (file:///projects/branches) + Running `target/debug/branches` +condition was true +``` + +Let’s try changing the value of `number` to a value that makes the condition +`false` to see what happens: + +```rust,ignore +let number = 7; +``` + +Run the program again, and look at the output: + +```bash +$ cargo run + Compiling branches v0.1.0 (file:///projects/branches) + Running `target/debug/branches` +condition was false +``` + +It’s also worth noting that `condition` here _must_ be a `bool`. To see what +happens if the condition isn't a `bool`, try running this code: + +```rust,ignore +fn main() { + let number = 3; + + if number { + println!("number was three"); + } +} +``` + +The `if` condition evaluates to a value of `3` this time, and Rust will +complain about it: + +```bash + Compiling branches v0.1.0 (file:///projects/branches) +error: mismatched types [--explain E0308] + --> src/main.rs:4:8 +4 |> if number { + |> ^^^^^^ expected bool, found integral variable +note: expected type `bool` +note: found type `_` + +error: aborting due to previous error +Could not compile `branches`. +``` + +The error tells us that Rust expected a `bool`, but got an integer. Rust will +not automatically try to convert non-boolean types to a boolean here, unlike +languages like Ruby or JavaScript. We must be explicit and always give `if` a +`boolean` as its condition. If your intention is for the `if` code block to be run if a number is not equal to `0`, for example, we would change the `if` expression to read: + +```rust +fn main() { + let number = 3; + + if number != 0 { + println!("number was something other than zero"); + } +} +``` + +Running this will print "number was something other than zero". + +#### Multiple Conditions with `else if` + +We can have multiple coniditions by combining `if` and `else` in an `else if` +expression. For example: + +```rust +fn main() { + let number = 5; + + if number == 3 { + println!("condition was 3"); + } else if number == 4 { + println!("condition was 4"); + } else if number == 5 { + println!("condition was 5"); + } else { + println!("condition was something else"); + } +} +``` + +This program has four possible paths it can take. If you try running it, you +should see output like this: + +```bash +$ cargo run + Compiling branches v0.1.0 (file:///projects/branches) + Running `target/debug/branches` +condition was 5 +``` + +When this program executes, it will check each `if` expression in turn and +execute the first body for which the condition holds true. + +Using too many `else if` expressions can clutter your code, so if you find +yourself with more than one, you may want to look at refactoring your code. In +Chapter XX, we'll talk about a powerful Rust branching construct called `match` +for these cases. + +#### Using `if` in a Binding + +The last detail you need to learn about `if` is that it’s an expression. That +means that we can use it on the right hand side of a `let` binding, for +instance: + +```rust +fn main() { + let condition = true; + let number = if condition { + 5 + } else { + 6 + }; + + println!("The value of number is: {}", number); +} +``` + +The `number` variable will be bound to a value based on the outcome of the `if` +expression. Let’s run this to see what happens: + +```bash +$ cargo run + Compiling branches v0.1.0 (file:///projects/branches) + Running `target/debug/branches` +The value of number is: 5 +``` + +Remember, blocks of code evaluate to the last expression in them, and numbers +by themselves are also expressions. In this case, the value of the whole `if` +expression depends on which block of code executes. This means that the value +that results from both arms of the `if` must be the same type; in the previous +example, they were both `i32` integers. But what happens if the types are +mismatched, as in the following example? + +```rust,ignore +fn main() { + let condition = true; + + let number = if condition { + 5 + } else { + "six" + }; + + println!("The value of number is: {}", number); +} +``` + +The expression in the `if` block is an integer and the expresion in the `else` +block is a string. This can’t work, because variable bindings must have a +single type. If we try to run this, we’ll get an error: + +```bash + Compiling branches v0.1.0 (file:///projects/branches) +src/main.rs:4:18: 8:6 error: if and else have incompatible types: + expected `_`, + found `&‘static str` +(expected integral variable, + found &-ptr) [E0308] +src/main.rs:4 let number = if condition { +src/main.rs:5 5 +src/main.rs:6 } else { +src/main.rs:7 "six" +src/main.rs:8 }; +src/main.rs:4:18: 8:6 help: run `rustc --explain E0308` to see a detailed explanation +error: aborting due to previous error +Could not compile `branches`. +``` + +The `if` and `else` arms have value types that are incompatible, and Rust tells +us exactly where to find the problem in our program. + +### Repetition with Loops + +It’s often useful to be able to execute a block of code more than one time. For +this, Rust has several constructs called *loops*. A loop runs through the code +inside it to the end and then starts immediately back at the beginning. To try +out loops, let’s make a new project. Navigate to your *projects* folder and use +Cargo to make a new project: + +```bash +$ cargo new --bin loops +$ cd loops +``` + +There are three kinds of loops in Rust: `loop`, `while`, and `for`. Let’s dig +in. + +#### Repeating Code with `loop` + +The `loop` keyword tells Rust to execute a block of code over and over again +forever or until we explicitly tell it to stop. + +For an example, change the *src/main.rs* file in your *loops* directory to look +like this: + +```rust,ignore +fn main() { + loop { + println!("again!"); + } +} +``` + +If we run this program, we’ll see `again!` printed over and over continuously +until we stop the program manually. Most terminals support a keyboard shortcut, +`control-c`, to halt a program stuck in a continual loop. Give it a try: + +```bash +$ cargo run + Compiling loops v0.1.0 (file:///projects/loops) + Running `target/debug/loops` +again! +again! +again! +again! +^Cagain! +``` + +That `^C` there is where we hit `control-c`. You may or may not see "again!" +printed after the `^C`, depending on where the code was in the loop when it +received the signal to halt. + +Fortunately, Rust provides another, more reliable way to break out of a loop. +We can place the `break` keyword within the loop to tell the program when to +stop executing the loop. Recall that we did this in the guessing game to exit +the program when the user won the game by guessing the number correctly. + +#### Conditional Loops With `while` + +A useful thing that many programs do is have a condition that can be evaluated +within a loop. While the condition is true, the loop runs. When the condition +ceases to be true, we call `break`, stopping the loop. This could be +implemented with a combination of `loop`, `if`, `else`, and `break`; try to do +that now if you'd like! + +But this pattern is so common that Rust has a more efficient language construct +for it, called a `while` loop. Here's an example using `while`: this program +loops three times, counting down each time. Finally, after the loop, it prints +another message, then exits: + +```rust +fn main() { + let mut number = 3; + + while number != 0 { + println!("{}!", number); + + number = number - 1; + } + + println!("LIFTOFF!!!"); +} +``` + +This gets rid of a lot of nesting that would be necessary if we used `loop`, +`if`, `else`, and `break`, and it's more clear. While a condition holds, run +this code; otherwise, exit the loop. + +#### Looping Though a Collection with `for` + +We could use this `while` construct to loop over the elements of a collection, +like an array. For example: + +```rust +fn main() { + let a = [10, 20, 30, 40, 50]; + let mut index = 0; + + while index < 5 { + println!("the value is: {}", a[index]); + + index = index + 1; + } +} +``` + +Here, we're counting up through the elements in the array. We start at index 0, +then loop until we hit the final index of our array (that is, when `index < 5` +is no longer true). Running this will print out every element of the array: + +```bash +$ cargo run + Compiling loops v0.1.0 (file:///projects/loops) + Running `target/debug/loops` +the value is: 10 +the value is: 20 +the value is: 30 +the value is: 40 +the value is: 50 +``` + +All five array values appear in the terminal, as expected. Even though `index` +will reach a value of `6` at some point, the loop stops executing before trying +to fetch a sixth value from the array. + +This approach is error-prone, though; we could cause our program to panic by +getting the index length incorrect. It's also slow, as the compiler needs to +perform the conditional check on every element on every iteration through the +loop. + +As a more efficient alternative, we can use a `for` loop and execute some code +for each item in a collection. A `for` loop looks like this: + +```rust +fn main() { + let a = [10, 20, 30, 40, 50]; + + for element in a.iter() { + println!("the value is: {}", element); + } +} +``` + +If we run this, we'll see the same output as the previous example. Importantly, +though, we've now increased the safety of our code and eliminated the chance of +bugs resulting from going beyond the end of the array or not going far enough +and missing some items. + +For example, in the previous code that uses the `while` loop, if we removed an +item from the `a` array but forgot to update the condition to be `while index < +4`, our code would panic. Using the `for` loop means we would not need to +remember to change any other code if we changed the the number of values in the +array. + +If you're wondering about the `.iter()` code in this example, keep reading! We +will cover method syntax generally in Chapter XX and iterators specifically in +Chapter XX. + +The safety and conciseness of `for` loops make them the most commonly used loop +construct in Rust. Even in situations where you want to run some code a certain +number of times, like our countdown example that used a `while` loop, most +Rustaceans would use a `for` loop. The way to do that is using a `Range`, which +is a type provided by the standard library that generates numbers starting from +one number and ending before another number. Here's what the countdown would +look like with a for loop, and using another method we haven't yet talked +about, `.rev()`, to reverse the range: + +```rust +fn main() { + for number in (1..4).rev() { + println!("{}!", number); + } + println!("LIFTOFF!!!"); +} +``` + +That's a bit nicer, isn't it? + +Now that you know how Rust does things that most other languages can do, let's +talk about a concept that _doesn't_ commonly exist: ownership. diff --git a/src/ch03-06-comments.md b/src/ch03-06-comments.md deleted file mode 100644 index 88995480d..000000000 --- a/src/ch03-06-comments.md +++ /dev/null @@ -1,39 +0,0 @@ -# Comments - -We strive to make our programs easy to understand, but sometimes, some extra -explanation is warranted. We can leave notes in our source code that the -compiler will ignore. These notes are called ‘comments’. - -Here’s a comment: - -```rust -// Hello, world. -``` - -Comments start with two slashes, and last until the end of the line. Larger -comments will need more lines: - -```rust -// So we’re doing something complicated here, long enough that we need -// multiple lines of comments to do it! Whew! Hopefully, this comment will -// explain what’s going on. -``` - -Comments can also go at the end of lines: - -```rust -fn main() { - let lucky_number = 7; // I’m feeling lucky today. -} -``` - -But you’ll more often see them above: - -```rust -fn main() { - // I’m feeling lucky today. - let lucky_number = 7; -} -``` - -That’s all there is to it. Comments are not particularly complicated. diff --git a/src/ch03-07-if.md b/src/ch03-07-if.md deleted file mode 100644 index 270491b72..000000000 --- a/src/ch03-07-if.md +++ /dev/null @@ -1,214 +0,0 @@ -# Control flow with `if` - -> Two roads diverged in a yellow wood, -> And sorry I could not travel both -> And be one traveler, long I stood -> And looked down one as far as I could -> To where it bent in the undergrowth; -> -> - Robert Frost, “The Road Not Taken” - -In Rust, there are a few ways to cause our code to branch. The most fundamental -way is by using `if`. An `if` expression gives us two paths forward, and asks -the question, “Which one should I take?” - -Let’s make a new project to explore `if`. Navigate to your projects directory, -and use Cargo to make a new project called `branches`: - -```bash -$ cargo new --bin branches -$ cd branches -``` - -Here’s a sample program using `if`: - -```rust -fn main() { - let condition = true; - - if condition { - println!("condition was true"); - } else { - println!("condition was false"); - } -} -``` - -Let's try running it: - -```bash -$ cargo run - Compiling branches v0.1.0 (file:///projects/branches) - Running `target/debug/branches` -condition was true -``` - -We can change the value of `condition`: - -```rust - let condition = false; -``` - -And then run it again: - -```bash -$ cargo run - Compiling branches v0.1.0 (file:///projects/branches) - Running `target/debug/branches` -condition was false -``` - -This is the very basic structure of `if`: _if_ the condition is true, then -execute some code. If it’s not true, then execute some other code, after -`else`. - -An `else` is not required: - -```rust -fn main() { - let condition = false; - - if condition { - println!("condition was true"); - } -} -``` - -In this case, nothing is printed. - -It’s also worth noting that `condition` here _must_ be a `bool`. Let’s try an -example with something else: - -```rust,ignore -fn main() { - let condition = 5; - - if condition { - println!("condition was five"); - } -} -``` - -If we try to run this program, Rust will complain: - -```bash - Compiling branches v0.1.0 (file:///projects/branches) -src/main.rs:4:8: 4:17 error: mismatched types: - expected `bool`, - found `_` -(expected bool, - found integral variable) [E0308] -src/main.rs:4 if condition { - ^~~~~~~~~ -src/main.rs:4:8: 4:17 help: run `rustc --explain E0308` to see a detailed explanation -error: aborting due to previous error -Could not compile `branches`. -``` - -We expected a `bool`, but got an integer. Rust will not automatically try to convert non-boolean types to a boolean here. We must be explicit. - -## `else if` - -We can make multiple decisions by combining `if` and `else` in another way: - -```rust -fn main() { - let number = 5; - - if number == 3 { - println!("condition was 3"); - } else if number == 4 { - println!("condition was 4"); - } else if number == 5 { - println!("condition was 5"); - } else { - println!("condition was something else"); - } -} -``` - -Let's try running it: - -```bash -$ cargo run - Compiling branches v0.1.0 (file:///projects/branches) - Running `target/debug/branches` -condition was 5 -``` - -When this program executes, it will check each `if` in turn, and execute the -first body for which the condition holds true. - -Using a single `else if` can be okay, but if you find yourself with more than one, -you may want to refactor your code. Rust has a more powerful branching construct -called `match` for these cases. We'll cover it later, when we talk about `enums`. - -## `if` as an expression - -There’s one last detail we need to learn about `if`: it’s an expression. That means -that we can use it on the right hand side of a `let` binding, for instance: - -```rust -fn main() { - let condition = true; - - let number = if condition { - 5 - } else { - 6 - }; - - println!("The value of number is: {}", number); -} -``` - -Let’s run this: - -```bash -$ cargo run - Compiling branches v0.1.0 (file:///projects/branches) - Running `target/debug/branches` -The value of number is: 5 -``` - -Remember, blocks of code evaluate to the last expression in them. And numbers -by themselves are also expressions. So in this case, the value of the whole -`if` expression depends on which block of code executes. - -There’s another small detail involved here: this means that if you use `if` -in this way, both arms of the `if` must be the same type. This doesn’t work: - -```rust,ignore -fn main() { - let condition = true; - - let number = if condition { - 5 - } else { - "six" - }; - - println!("The value of number is: {}", number); -} -``` - -If we try to run this, we’ll get an error: - -```bash - Compiling branches v0.1.0 (file:///projects/branches) -src/main.rs:4:18: 8:6 error: if and else have incompatible types: - expected `_`, - found `&‘static str` -(expected integral variable, - found &-ptr) [E0308] -src/main.rs:4 let number = if condition { -src/main.rs:5 5 -src/main.rs:6 } else { -src/main.rs:7 "six" -src/main.rs:8 }; -src/main.rs:4:18: 8:6 help: run `rustc --explain E0308` to see a detailed explanation -error: aborting due to previous error -Could not compile `branches`. -``` - -`if` and `else` have incompatible types. This can’t work. diff --git a/src/ch03-08-loops.md b/src/ch03-08-loops.md deleted file mode 100644 index f9955c75b..000000000 --- a/src/ch03-08-loops.md +++ /dev/null @@ -1,197 +0,0 @@ -# Loops - -It’s often quite useful to be able to execute a block of code more than one -time. For this, we have several constructs, called ‘loops’. - -To try out loops, let’s make a new project. Navigate to your projects folder -and use Cargo to make a new one: - -```bash -$ cargo new --bin loops -$ cd loops -``` - -There are three kinds of loops in Rust: `loop`, `while`, and `for`. Let’s dig -in. - -## `loop` - -The `loop` keyword is very straightforward: it executes a block of code over -and over and over and over and over and over forever. Change your `src/main.rs` -file to look like this: - -```rust,ignore -fn main() { - loop { - println!("again!"); - } -} -``` - -If we run this program, we’ll see ‘`again!`’ printed over and over again. So -how does our program end? It doesn’t, until we kill it. Most terminals support -a keyboard shortcut, ‘control-c’, to stop a runaway program. Give it a try: - -```bash -$ cargo run - Compiling loops v0.1.0 (file:///projects/loops) - Running `target/debug/loops` -again! -again! -again! -again! -^Cagain! -``` - -That `^C` there is where I hit control-c. - -That’s a lot of trouble though! Luckily, there’s a way to break an infinite `loop`. - -### Breaking out of a loop - -The `break` keyword will allow us to quit looping. Try this version out: - -```rust -fn main() { - loop { - println!("once!"); - break; - } -} -``` - -If you run this program with `cargo run`, you’ll see that it only executes one -time: - -```bash -$ cargo run - Compiling loops v0.1.0 (file:///projects/loops) - Running `target/debug/loops` -once! -``` - -When a Rust program hits a `break` statement, it will exit the current loop. - -## `while` - -What if we took `loop`, `break`, and `if`, and put them together? Something -like this: - -```rust -fn main() { - let mut number = 3; - - loop { - if number != 0 { - println!("{}!", number); - - number = number - 1; - } else { - break; - } - - } - - println!("LIFTOFF!!!"); -} -``` - -If we run this, we’ll get some output: - -```bash - Compiling loops v0.1.0 (file:///projects/loops) - Running `target/debug/loops` -3! -2! -1! -LIFTOFF!!! -``` - -The core of this example is in the combination of these three constructs: - -```rust,ignore - loop { - if number != 0 { - // do stuff - } else { - break; - } -``` - -We want to `loop`, but only while some sort of condition is true. As soon as it -isn't, we want to `break` out of the loop. - -This pattern is so common that we have a language construct for it: `while`. -Here's the same example, but using `while` instead: - -```rust -fn main() { - let mut number = 3; - while number != 0 { - println!("{}!", number); - - number = number - 1; - } - - println!("LIFTOFF!!!"); -} -``` - -This lets us get rid of a lot of nesting, and is more clear: while a condition holds, -run this code. - -## `for` - -We can use this `while` construct to loop over the elements of a collection, like an -array: - -```rust -fn main() { - let a = [1, 2, 3, 4, 5]; - let mut index = 0; - - while index < 5 { - println!("the value is: {}", a[index]); - - index = index + 1; - } -} -``` - -Running this will print out every element of the array: - -```bash -$ cargo run - Compiling loops v0.1.0 (file:///projects/loops) - Running `target/debug/loops` -the value is: 1 -the value is: 2 -the value is: 3 -the value is: 4 -the value is: 5 -``` - -Here, we're counting up instead of down: we start at zero, then loop until we -hit the final index of our array. - -This approach is error-prone, though. If we get the index length incorrect, we -will end up causing a `panic!`. This is also slow, as the compiler needs to do -that check on every element on every iteration through the loop. - -Instead, we can use our last kind of loop: the `for` loop. It looks like this: - -```rust -fn main() { - let a = [1, 2, 3, 4, 5]; - - for element in a.iter() { - println!("the value is: {}", element); - } -} -``` - -** NOTE: see [https://github.com/rust-lang/rust/issues/25725#issuecomment-166365658](https://github.com/rust-lang/rust/issues/25725#issuecomment-166365658), we may want to change this ** - -If we run this, we'll see the same output as the previous example. - -** I'm going to leave it at this for now until we decide how we want to do it** diff --git a/src/ch04-00-understanding-ownership.md b/src/ch04-00-understanding-ownership.md new file mode 100644 index 000000000..ed110878f --- /dev/null +++ b/src/ch04-00-understanding-ownership.md @@ -0,0 +1,6 @@ +# Understanding Ownership + +Ownership is important to understand: it's Rust's most unique feature, and +enables Rust to make memory safety guarantees without needing a garbage +collector. We’ll also talk about several related features: borrowing, slices, +and how Rust lays things out in memory. diff --git a/src/ch04-02-ownership.md b/src/ch04-01-ownership.md similarity index 54% rename from src/ch04-02-ownership.md rename to src/ch04-01-ownership.md index b0e5b3c4e..2cd1f0eb0 100644 --- a/src/ch04-02-ownership.md +++ b/src/ch04-01-ownership.md @@ -1,49 +1,45 @@ -# Ownership +## Ownership Rust’s central feature is called ‘ownership’. It is a feature that is straightforward to explain, but has deep implications for the rest of the language. -Rust is committed to both safety and speed. One of the key tools for balancing -between them is “zero-cost abstractions”: the various abstractions in Rust do -not pose a global performance penalty. The ownership system is a prime example -of a zero-cost abstraction. All of the analysis we’ll talk about in this guide -is done at compile time. You do not pay any run-time cost for any of these +All programs have to manage the way they use a computer's memory while running. +Some languages have garbage collection, while in others, the programmer has to +explicitly allocate and free the memory. Rust takes a third approach: memory is +managed through a system of ownership with a set of rules that the compiler +checks at compile-time. You do not pay any run-time cost for any of these features. -However, this system does have a certain cost: learning curve. Many new -Rustaceans experience something we like to call ‘fighting with the borrow -checker’, where the Rust compiler refuses to compile a program that the author -thinks is valid. This can happen because the programmer isn’t used to thinking -carefully about ownership, or is thinking about it differently from the way -that Rust does. You probably will experience something similar at first. There is -good news, however: more experienced Rust developers report that once they work -with the rules of the ownership system for a period of time, they fight the -borrow checker less and less. Keep at it! +However, because ownership is a new concept for many programmers, it does take +some time to get used to. There is good news, though: the more experienced you +become with Rust and the rules of the ownership system, the more you'll be +able to naturally develop code that is both safe and efficient. Keep at it! -This chapter will give you a foundation for understanding the rest of the -language. To do so, we’re going to learn through examples, focusing on a very -common data structure: strings. +Once you understand ownership, you have a good foundation for understanding the +features that make Rust unique. In this chapter, we'll learn ownership by going +through some examples, focusing on a very common data structure: strings. -## Variable binding scope +### Variable binding scope -Let’s take a step back and look at the very basics again. Now that we’re past -basic syntax, we won’t include all of the `fn main() {` stuff in examples, so -if you’re following along, you will have to put them inside of a `main()` -function. This lets our examples be a bit more concise, letting us focus on the -actual details, rather than boilerplate. +We've walked through an example of a Rust program already in the tutorial +chapter. Now that we’re past basic syntax, we won’t include all of the `fn +main() {` stuff in examples, so if you’re following along, you will have to put +them inside of a `main()` function. This lets our examples be a bit more +concise, letting us focus on the actual details rather than boilerplate. -Anyway, here it is: +Anyway, here is our first example: ```rust let s = "hello"; ``` -This variable binding refers to a string literal. It’s valid from the point at -which it’s declared, until the end of the current _scope_. That is: +This variable binding refers to a string literal, where the value of the string +is hard coded into the text of our program. The binding is valid from the point +at which it’s declared until the end of the current _scope_. That is: ```rust -{ // s is not valid here, it’s not yet in scope +{ // s is not valid here, it’s not yet declared let s = "hello"; // s is valid from this point forward // do stuff with s @@ -55,36 +51,38 @@ In other words, there are two important points in time here: - When `s` comes ‘into scope’, it is valid. - It remains so until it ‘goes out of scope’. -At this point, things are similar to other programming languages. Let’s build -on top of this understanding by introducing a new type: `String`. +At this point, things are similar to other programming languages. Now let’s +build on top of this understanding by introducing the `String` type. -## Strings +### Strings -String literals are convenient, but they aren’t the only way that you use strings. -For one thing, they’re immutable. For another, not every string is literal: -what about taking user input and storing it in a string? +String literals are convenient, but they aren’t the only way that you use +strings. For one thing, they’re immutable. For another, not every string is +literal: what about taking user input and storing it in a string? -For this, Rust has a second string type, `String`. You can create a `String` from -a string literal using the `from` function: +For this, Rust has a second string type, `String`. You can create a `String` +from a string literal using the `from` function: ```rust let s = String::from("hello"); ``` -We haven’t seen the double colon (`::`) syntax yet. It is a kind of scope -operator, allowing us to namespace this particular `from()` function under the -`String` type itself, rather than using some sort of name like `string_from()`. -We’ll discuss this syntax more in the “Method Syntax” and “Modules” chapters. +We haven’t seen the double colon (`::`) syntax yet. It is an operator that +allows us to namespace this particular `from()` function under the `String` +type itself, rather than using some sort of name like `string_from()`. We’ll +discuss this syntax more in the “Method Syntax” and “Modules” chapters. This kind of string can be mutated: ```rust let mut s = String::from("hello"); -s.push_str(", world!"); +s.push_str(", world!"); // push_str() appends a literal to a String + +println!("{}", s); // This will print `hello, world!` ``` -## Memory and allocation +### Memory and allocation So, what’s the difference here? Why can `String` be mutated, but literals cannot? The difference comes down to how these two types deal with memory. @@ -92,11 +90,13 @@ cannot? The difference comes down to how these two types deal with memory. In the case of a string literal, because we know the contents of the string at compile time, we can hard-code the text of the string directly into the final executable. This means that string literals are quite fast and efficient. But -these properties only come from its immutability; we can’t put an -arbitrary-sized blob of memory into the binary for each string! +these properties only come from its immutability. Unfortunately, we can’t put a +blob of memory into the binary for each string whose size is unknown at compile +time and whose size might change over the course of running the program. -With `String`, to support a mutable, growable string, we need to allocate an -unknown amount of memory to hold the contents. This means two things: +With `String`, in order to support a mutable, growable string, we need to +allocate an unknown amount of memory to hold the contents. This means two +things: 1. The memory must be requested from the operating system at runtime. 2. We need a way of giving this memory back to the operating system when we’re @@ -107,13 +107,15 @@ implementation requests the memory it needs. This is pretty much universal in programming languages. The second case, however, is different. In languages with a garbage collector -(‘GC’), the GC handles that second case, and we, as the programmer, don’t need -to think about it. Without GC, it’s the programmer’s responsibility to identify -when memory is no longer being used, and explicitly return it, just as it was -requested. Doing this correctly has historically been a difficult problem. If -we forget, we will waste memory. If we do it too early, we will have an invalid -variable. If we do it twice, that’s a bug too. We need to pair exactly one -`allocate()` with exactly one `free()`. +(‘GC’), the GC will keep track and clean up memory that isn't being used +anymore, and we, as the programmer, don’t need to think about it. Without GC, +it’s our responsibility to identify when memory is no longer being used and +call code to explicitly return it, just as we did to request it. Doing this +correctly has historically been a difficult problem. If we forget, we will +waste memory. If we do it too early, we will have an invalid variable. If we do +it twice, that’s a bug too. We need to pair exactly one `allocate()` with +exactly one `free()`. + Rust takes a different path. Remember our example? Here’s a version with `String`: @@ -126,10 +128,11 @@ Rust takes a different path. Remember our example? Here’s a version with } // this scope is now over, and s is no longer valid ``` -We have a natural point at which we can return the memory our `String` needs back -to the operating system: when it goes out of scope! When a variable goes out of -scope, a special function is called. This function is called `drop()`, and it -is where the author of `String` can put the code to return the memory. +We have a natural point at which we can return the memory our `String` needs +back to the operating system: when it goes out of scope. When a variable goes +out of scope, Rust calls a special function for us. This function is called +`drop()`, and it is where the author of `String` can put the code to return the +memory. > Aside: This pattern is sometimes called “Resource Acquisition Is > Initialization” in C++, or “RAII” for short. While they are very similar, @@ -138,10 +141,10 @@ is where the author of `String` can put the code to return the memory. > is _roughly_ similar in Rust, but not identical. This pattern has a profound impact on the way that Rust code is written. It may -seem obvious right now, but things can get tricky in more advanced situations! +seem obvious right now, but things can get tricky in more advanced situations. Let’s go over the first one of those right now. -## Move +### Move What would you expect this code to do? @@ -150,7 +153,7 @@ let x = 5; let y = x; ``` -You might say “Make a copy of `5`.” That’d be correct! We now have two +You might say “Make a copy of `5`”, and that would be correct. We now have two bindings, `x` and `y`, and both equal `5`. Now let’s look at `String`. What would you expect this code to do? @@ -170,19 +173,12 @@ A `String` is made up of three parts: a pointer to the memory that holds the contents of the string, a length, and a capacity. The length is how much memory the `String` is currently using. The capacity is the total amount of memory the `String` has gotten from the operating system. The difference between length -and capacity matters, but not in this context, so don’t worry about it too much -if it doesn’t make sense, and just ignore the capacity. +and capacity matters but not in this context, so don’t worry about it too much. +For right now, it's fine to ignore the capacity. -> We’ve talked about two kinds of composite types: arrays and tuples. `String` -> is a third type: a `struct`, which we will cover the details of in the next -> chapter of the book. For now, thinking about `String` as a tuple is close -> enough. - -When we assign `s1` to `s2`, the `String` itself is copied. But not all kinds -of copying are the same. Many people draw distinctions between ‘shallow -copying’ and ‘deep copying’. We don’t use these terms in Rust. We instead say -that something is ‘moved’ or ‘cloned’. Assignment in Rust causes a ‘move’. In -other words, it looks like this: +When we assign `s1` to `s2`, the `String` itself is copied, meaning we copy the +pointer, the length, and the capacity. We do not copy the data that the +`String`'s pointer refers to. In other words, it looks like this: s1 and s2 @@ -190,31 +186,33 @@ _Not_ this: s1 and s2 to two places -When moving, Rust makes a copy of the data structure itself, the contents of -`s1` are copied, but if `s1` contains a reference, like it does in this case, -Rust will not copy the things that those references refer to. +There’s a problem here. Both data pointers are pointing to the same place. Why +is this a problem? Well, when `s2` goes out of scope, it will free the memory +that the pointer points to. And then `s1` goes out of scope, and it will _also_ +try to free the memory that the pointer points to. That’s bad, and is known as +a "double free" error. -There’s a problem here! Both `data` pointers are pointing to the same place. -Why is this a problem? Well, when `s2` goes out of scope, it will free the -memory that `data` points to. And then `s1` goes out of scope, and it will -_also_ try to free the memory that `data` points to! That’s bad. +So what’s the solution? Here, we stand at a crossroads with a few options. -So what’s the solution? Here, we stand at a crossroads. There are a few -options. One would be to declare that assignment will also copy out any data. -This works, but is inefficient: what if our `String` contained a novel? Also, -it only works for memory. What if, instead of a `String`, we had a -`TcpConnection`? Opening and closing a network connection is very similar to -allocating and freeing memory. The solution that we could use there is to allow -the programmer to hook into the assignment, similar to `drop()`, and write code -to fix things up. That would work, but now, an `=` can run arbitrary code. That’s -also not good, and it doesn’t solve our efficiency concerns either. +One way would be to change assignment so that it will also copy out any data. +This works, but is inefficient: what if our `String` contained a novel? +Also, that solution would only work for memory. What if, instead of a `String`, +we had a `TcpConnection`? Opening and closing a network connection is very +similar to allocating and freeing memory, so it would be nice to be able to use +the same mechanism. We wouldn't be able to, though, because creating a new +connection requires more than just copying memory: we have to request a new +connection from the operating system. We could then extend our solution to +allow the programmer to hook into the assignment, similar to `drop()`, and +write code to fix things up. That would work, but if we did that, an `=` could +run arbitrary code. That’s also not good, and it doesn’t solve our efficiency +concerns either. Let’s take a step back: the root of the problem is that `s1` and `s2` both -think that they have control of the memory, and therefore needs to free it. +think that they have control of the memory and therefore need to free it. Instead of trying to copy the allocated memory, we could say that `s1` is no -longer valid, and therefore, doesn’t need to free anything. This is in fact the -choice that Rust makes. Check it out what happens when you try to use `s1` -after `s2` is created: +longer valid and, therefore, doesn’t need to free anything. This is in fact the +choice that Rust makes. Check out what happens when you try to use `s1` after +`s2` is created: ```rust,ignore let s1 = String::from("hello"); @@ -225,7 +223,7 @@ println!("{}", s1); You’ll get an error like this: -```text +```bash 5:22 error: use of moved value: `s1` [E0382] println!("{}", s1); ^~ @@ -235,30 +233,35 @@ println!("{}", s1); ^~ ``` -We say that `s1` was _moved_ into `s2`. When a value moves, its data is copied, -but the original variable binding is no longer usable. That solves our problem: +If you have heard the terms "shallow copy" and "deep copy" while working with +other languages, the concept of copying the pointer, length, and capacity +without copying the data probably sounded like a shallow copy. Because Rust +also invalidates the first binding, instead of calling this a shallow copy, +it's called a _move_. Here we would read this by saying that `s1` was _moved_ +into `s2`. So what actually happens looks like this: s1 and s2 to the same place -With only `s2` valid, when it goes out of scope, it will free the memory, and we’re done! +That solves our problem! With only `s2` valid, when it goes out of scope, it +alone will free the memory, and we’re done. -## Ownership Rules +### Ownership Rules This leads us to the Ownership Rules: -> 1. Each value in Rust has a variable binding that’s called it’s ‘owner’. +> 1. Each value in Rust has a variable binding that’s called its ‘owner’. > 2. There can only be one owner at a time. > 3. When the owner goes out of scope, the value will be `drop()`ped. Furthermore, there’s a design choice that’s implied by this: Rust will never -automatically create ‘deep’ copies of your data. Any automatic copying must be -inexpensive. +automatically create ‘deep’ copies of your data. Therefore, any _automatic_ +copying can be assumed to be inexpensive. -## Clone +### Clone -But what if we _do_ want to deeply copy the `String`’s data, and not just the +But what if we _do_ want to deeply copy the `String`’s data and not just the `String` itself? There’s a common method for that: `clone()`. We will discuss -methods in the next section on [`structs`], but they’re a common enough feature +methods in the section on [`structs`], but they’re a common enough feature in many programming languages that you have probably seen them before. Here’s an example of the `clone()` method in action: @@ -278,10 +281,10 @@ it _is_ doing this: s1 and s2 to two places When you see a call to `clone()`, you know that some arbitrary code is being -executed, which may be expensive. It’s a visual indicator that something +executed, and that code may be expensive. It’s a visual indicator that something different is going on here. -## Copy +### Copy There’s one last wrinkle that we haven’t talked about yet. This code works: @@ -294,38 +297,32 @@ println!("{}", x); But why? We don’t have a call to `clone()`. Why didn’t `x` get moved into `y`? -For types that do not have any kind of complex storage requirements, like -integers, typing `clone()` is busy work. There’s no reason we would ever want -to prevent `x` from being valid here, as there’s no situation in which it’s -incorrect. In other words, there’s no difference between deep and shallow -copying here, so calling `clone()` wouldn’t do anything differently from the -usual shallow copying. +Types like integers that have a known size at compile time do not ask for +memory from the operating system and therefore do not need to be `drop()`ped +when they go out of scope. That means there's no reason we would want to +prevent `x` from being valid after we create the binding `y`. In other words, +there’s no difference between deep and shallow copying here, so calling +`clone()` wouldn’t do anything differently from the usual shallow copying and +we can leave it out. -Rust has a special annotation that you can place on types, called `Copy`. If -a type is `Copy`, an older binding is still usable after assignment. Integers -are an example of such a type; most of the primitive types are `Copy`. - -While we haven’t talked about how to mark a type as `Copy` yet, you might ask -yourself “what happens if we made `String` `Copy`?” The answer is, you cannot. -Remember `drop()`? Rust will not let you make something `Copy` if it has +Rust has a special annotation that you can place on types like these, and that +annotation is the `Copy` trait. We'll talk more about traits in Chapter XX. If +a type has the `Copy` trait, an older binding is still usable after assignment. +Rust will not let you make something have the `Copy` trait if it has implemented `drop()`. If you need to do something special when the value goes out of scope, being `Copy` will be an error. So what types are `Copy`? You can check the documentation for the given type to -be sure, but as a rule of thumb, any group of simple scalar values can be -Copy, but nothing that requires allocation or is some form of resource is `Copy`. -And you can’t get it wrong: the compiler will throw an error if you try to use -a type that moves incorrectly, as we saw above. - -Here’s some types that you’ve seen so far that are `Copy`: +be sure, but as a rule of thumb, any group of simple scalar values can be Copy, +but nothing that requires allocation or is some form of resource is `Copy`. Here’s some of the types that are `Copy`: * All of the integer types, like `u32`. * The booleans, `true` and `false`. * All of the floating point types, like `f64`. * Tuples, but only if they contain types which are also `Copy`. `(i32, i32)` - is `Copy`, but `(i32, String)` is not! + is `Copy`, but `(i32, String)` is not. -## Ownership and functions +### Ownership and functions Passing a value to a function has similar semantics as assigning it: @@ -379,7 +376,9 @@ fn makes_copy(some_integer: i32) { // some_integer comes into scope. ``` Remember: If we tried to use `s` after the call to `takes_ownership()`, Rust -would throw a compile-time error! These static checks protect us from mistakes. +would throw a compile-time error. These static checks protect us from mistakes. +Try adding code to `main` that uses `s` and `x` to see where you can use them +and where the ownership rules prevent you from doing so. Returning values can also transfer ownership: @@ -440,9 +439,11 @@ fn takes_and_gives_back(a_string: String) -> String { // a_string comes into sco It’s the same pattern, every time: assigning something moves it, and when an owner goes out of scope, if it hasn’t been moved, it will `drop()`. -This might seem a bit tedious, and it is. What if I want to let a function use -a value, but not take ownership? It’s quite annoying that anything I pass in -also needs to be passed back. Look at this function: +This might seem a bit tedious, and it is. What if we want to let a function use +a value but not take ownership? It’s quite annoying that anything we pass in +also needs to be passed back if we want to use it again, in addition to any +data resulting from the body of the function that we might want to return as +well. It's _possible_ to return multiple values, using a tuple, like this: ```rust fn main() { @@ -460,7 +461,6 @@ fn calculate_length(s: String) -> (String, usize) { } ``` -This is too much ceremony: we have to use a tuple to give back the `String` as -well as the length. It’s a lot of work for a pattern that should be common. - -Luckily for us, Rust has such a feature, and it’s what the next section is about. +But this is too much ceremony and a lot of work for a concept that should be +common. Luckily for us, Rust has a feature for this concept, and it’s what the +next section is about. diff --git a/src/ch04-01-understanding-ownership.md b/src/ch04-01-understanding-ownership.md deleted file mode 100644 index 2b2eb4332..000000000 --- a/src/ch04-01-understanding-ownership.md +++ /dev/null @@ -1,6 +0,0 @@ -# Understanding Ownership - -Now that we’ve got some basic syntax under our belt, it’s time to take a look -at Rust’s most unique feature: ownership. We’ll also talk about several related -features: borrowing, slices, and lifetimes, as well as how Rust lays things out -in memory. diff --git a/src/ch04-03-references-and-borrowing.md b/src/ch04-02-references-and-borrowing.md similarity index 62% rename from src/ch04-03-references-and-borrowing.md rename to src/ch04-02-references-and-borrowing.md index b53dca7e4..6c0f3d828 100644 --- a/src/ch04-03-references-and-borrowing.md +++ b/src/ch04-02-references-and-borrowing.md @@ -1,4 +1,4 @@ -# References and Borrowing +## References and Borrowing At the end of the last section, we had some example Rust that wasn’t very good. Here it is again: @@ -20,7 +20,8 @@ fn calculate_length(s: String) -> (String, usize) { ``` The issue here is that we have to return the `String` back to the calling -function so that it could still use it. +function so that we can still use it there, since it was moved when we called +`calculate_length()`. There is a better way. It looks like this: @@ -40,11 +41,12 @@ fn calculate_length(s: &String) -> usize { } ``` -First, you’ll notice all of the tuple stuff is gone. Next, that we pass `&s1` -into `calculate_lengths()`. And in its definition, we take `&String` rather -than `String`. +First, you’ll notice all of the tuple stuff in the binding declaration and the +function return value is gone. Next, note that we pass `&s1` into +`calculate_length()`, and in its definition, we take `&String` rather than +`String`. -These `&s` are called ‘references’, and they allow you to refer to some value +These `&`s are called ‘references’, and they allow you to refer to some value without taking ownership of it. Here’s a diagram: DIAGRAM GOES HERE of a &String pointing at a String, with (ptr, len, capacity) @@ -62,14 +64,12 @@ let s1 = String::from("hello"); let len = calculate_length(&s1); ``` -The `&s1` syntax lets us create a reference from `s1`. This reference _refers_ -to the value of `s1`, but does not own it. Because it does not own it, the +The `&s1` syntax lets us create a reference with `s1`. This reference _refers_ +to the value of `s1` but does not own it. Because it does not own it, the value it points to will not be dropped when the reference goes out of scope. Likewise, the signature of the function uses `&` to indicate that it takes -a reference as an argument: - -Let’s add some explanatory annotations: +a reference as an argument. Let’s add some explanatory annotations: ```rust fn calculate_length(s: &String) -> usize { // s is a reference to a String @@ -86,11 +86,11 @@ This lets us write functions which take references as arguments instead of the values themselves, so that we won’t need to return them to give back ownership. There’s another word for what references do, and that’s ‘borrowing’. Just like -with real life, if I own something, you can borrow it from me. When you’re done, -you have to give it back. +with real life, if a person owns something, you can borrow it from them. When +you’re done, you have to give it back. Speaking of which, what if you try to modify something you borrow from me? Try -this code out. Spoiler alert: it doesn’t work: +this code out. Spoiler alert: it doesn’t work! ```rust,ignore fn main() { @@ -100,22 +100,24 @@ fn main() { } fn change(some_string: &String) { - some_string.push_str(", world"); // push_str() appends a literal to a String + some_string.push_str(", world"); } ``` Here’s the error: ```text -8:16 error: cannot borrow immutable borrowed content `*some_string` as mutable - some_string.push_str(", world"); // push_str() appends a literal to a String - ^~~~~~~~~~~ +error: cannot borrow immutable borrowed content `*some_string` as mutable + --> error.rs:8:5 + | +8 | some_string.push_str(", world"); + | ^^^^^^^^^^^ ``` -Just like bindings are immutable by default, so are references. We’re not allowed -to modify something we have a reference to. +Just like bindings are immutable by default, so are references. We’re not +allowed to modify something we have a reference to. -## Mutable references +### Mutable references We can fix this bug! Just a small tweak: @@ -127,12 +129,13 @@ fn main() { } fn change(some_string: &mut String) { - some_string.push_str(", world"); // push_str() appends a literal to a String + some_string.push_str(", world"); } ``` -First, we had to change `s` to be `mut`. Then, we had to create a mutable reference -with `&mut s` and accept a mutable reference with `some_string: &mut String`. +First, we had to change `s` to be `mut`. Then we had to create a mutable +reference with `&mut s` and accept a mutable reference with `some_string: &mut +String`. Mutable references have one big restriction, though. This code fails: @@ -146,28 +149,24 @@ let r2 = &mut s; Here’s the error: ```text -5:20 error: cannot borrow `s` as mutable more than once at a time [E0499] - let r2 = &mut s; - ^ -4:20 note: previous borrow of `s` occurs here; the mutable borrow prevents - subsequent moves, borrows, or modification of `s` until the borrow - ends - let r1 = &mut s; - ^ -7:2 note: previous borrow ends here -fn main() { - -} -^ +error[E0499]: cannot borrow `s` as mutable more than once at a time + --> borrow_twice.rs:5:19 + | +4 | let r1 = &mut s; + | - first mutable borrow occurs here +5 | let r2 = &mut s; + | ^ second mutable borrow occurs here +6 | } + | - first borrow ends here ``` -The error is what it says on the tin: you cannot borrow something more than -once at a time in a mutable fashion. This restriction allows for mutation, but -in a very controlled fashion. It is something that new Rustaceans struggle -with, because most languages let you mutate whenever you’d like. +The error is what it says: you cannot borrow something mutably more than once +at a time. This restriction allows for mutation but in a very controlled +fashion. It is something that new Rustaceans struggle with, because most +languages let you mutate whenever you’d like. As always, we can use `{}`s to create a new scope, allowing for multiple mutable -references. Just not _simultaneous_ ones: +references, just not _simultaneous_ ones: ```rust let mut s = String::from("hello"); @@ -180,7 +179,8 @@ let mut s = String::from("hello"); let r2 = &mut s; ``` -There is a similar rule for combining the two kinds of references. This code errors: +There is a similar rule for combining mutable and immutable references. This +code errors: ```rust,ignore let mut s = String::from("hello"); @@ -193,27 +193,23 @@ let r3 = &mut s; // BIG PROBLEM Here’s the error: ```text -19: 6:20 error: cannot borrow `s` as mutable because it is also borrowed as - immutable [E0502] - let r3 = &mut s; // BIG PROBLEM - ^ -15: 4:16 note: previous borrow of `s` occurs here; the immutable borrow - prevents subsequent moves or mutable borrows of `s` until the - borrow ends - let r1 = &s; // no problem - ^ -8:2 note: previous borrow ends here -fn main() { - -} -^ +error[E0502]: cannot borrow `s` as mutable because it is also borrowed as immutable + --> borrow_thrice.rs:6:19 + | +4 | let r1 = &s; // no problem + | - immutable borrow occurs here +5 | let r2 = &s; // no problem +6 | let r3 = &mut s; // BIG PROBLEM + | ^ mutable borrow occurs here +7 | } + | - immutable borrow ends here ``` Whew! We _also_ cannot have a mutable reference while we have an immutable one. Users of an immutable reference don’t expect the values to suddenly change out from under them! Multiple immutable references are okay, however. -## Dangling references +### Dangling references In languages with pointers, it’s easy to create a “dangling pointer” by freeing some memory while keeping around a pointer to that memory. In Rust, by @@ -238,12 +234,17 @@ fn dangle() -> &String { Here’s the error: ```text -error: missing lifetime specifier [E0106] -fn dangle() -> &String { - ^~~~~~~ -help: this function’s return type contains a borrowed value, but there is no - value for it to be borrowed from -help: consider giving it a ‘static lifetime +error[E0106]: missing lifetime specifier + --> dangle.rs:5:16 + | +5 | fn dangle() -> &String { + | ^^^^^^^ + | + = help: this function's return type contains a borrowed value, but there is no + value for it to be borrowed from + = help: consider giving it a 'static lifetime + +error: aborting due to previous error ``` This error message refers to a feature we haven’t learned about yet, @@ -284,30 +285,13 @@ fn no_dangle() -> String { This works, no problem. Ownership is moved out, nothing is deallocated. -## The Rules of References +### The Rules of References Here’s a recap of what we’ve talked about: 1. At any given time, you may have _either_, but not both of: 1. One mutable reference. - 2. Any number of immutable references . + 2. Any number of immutable references. 2. References must always be valid. -While these rules are not complicated on their own, they can be tricky when -applied to real code. Let’s work through a number of examples to help build -our understanding. - -## More Examples - -COMING SOON - - - - - - - - - - - +Next, let's look at a different kind of reference: slices. diff --git a/src/ch04-04-slices.md b/src/ch04-03-slices.md similarity index 58% rename from src/ch04-04-slices.md rename to src/ch04-03-slices.md index 3d028635d..10228a983 100644 --- a/src/ch04-04-slices.md +++ b/src/ch04-03-slices.md @@ -1,11 +1,11 @@ -# Slices +## Slices So far, we’ve talked about types that have ownership, like `String`, and ones -that don’t, like `&String`. There is a second kind of type which does not have +that don’t, like `&String`. There is another kind of type which does not have ownership: slices. Slices let you reference a contiguous sequence of elements -in a collection, rather than the whole collection itself. +in a collection rather than the whole collection itself. -Here’s a small programming problem: write a function which takes a string, +Here’s a small programming problem: write a function which takes a string and returns the first word you find. If we don’t find a space in the string, then the whole string is a word, so the whole thing should be returned. @@ -24,8 +24,8 @@ the word, though. Let’s try that: fn first_word(s: &String) -> usize { let bytes = s.as_bytes(); - for (i, &byte) in bytes.iter().enumerate() { - if byte == 32 { + for (i, &item) in bytes.iter().enumerate() { + if item == 32 { return i; } } @@ -36,42 +36,43 @@ fn first_word(s: &String) -> usize { Let’s break that down a bit: -```rust -fn first_word(s: &String) -> usize { - - // Since we need to go through the String element by element, and - // check if a value is a space, we will convert our String to an - // array of bytes, using the `.as_bytes()` method. - let bytes = s.as_bytes(); - - // We discussed using the iter() method with for in Chapter 3.7. Here, - // we’re adding another method: enumerate(). While iter() returns each - // element, enumerate() modifies the result of iter(), and returns a - // tuple instead. The first element of the tuple is the index, and the - // second element is a reference to the element itself. This is a bit - // nicer than calculating the index ourselves. - // - // Since it’s a tuple, we can use patterns, just like elsewhere in Rust. - // So we match against the tuple with i for the index, and &byte for - // the byte itself. - for (i, &byte) in bytes.iter().enumerate() { - - // 32 is the value of a space in UTF-8 - if byte == 32 { - - // We found a space! Return this position. - return i; - } - } - - // If we got here, we didn’t find a space, so this whole thing must be a - // word. So return the length. - s.len() -} +```rust,ignore +let bytes = s.as_bytes(); ``` +Since we need to go through the String element by element and +check if a value is a space, we will convert our String to an +array of bytes using the `.as_bytes()` method. + +```rust,ignore +for (i, &item) in bytes.iter().enumerate() { +``` + +We will be discussing iterators in more detail in Chapter XX, but for +now, know that `iter()` is a method that returns each element in a +collection, and `enumerate()` modifies the result of `iter()` and returns +a tuple instead. The first element of the tuple is the index, and the +second element is a reference to the element itself. This is a bit +nicer than calculating the index ourselves. + +Since it’s a tuple, we can use patterns, just like elsewhere in Rust. So we +match against the tuple with i for the index and &item for a single byte. Since +we get a reference from `.iter().enumerate()`, we use `&` in the pattern. + +```rust,ignore + if item == 32 { + return i; + } +} +s.len() +``` + +We search for the value 32, which represents a space in UTF-8. If we find one, +we return the position. Otherwise, we return the length of the string, using +`s.len()`. + This works, but there’s a problem. We’re returning a `usize` on its own, but -it’s only a meaningful number in the context of the `&String` itself. In other +it’s only a meaningful number in the context of the `&String`. In other words, because it’s a separate value from the `String`, there’s no guarantee that it will still be valid in the future. Consider this: @@ -79,8 +80,8 @@ that it will still be valid in the future. Consider this: # fn first_word(s: &String) -> usize { # let bytes = s.as_bytes(); # -# for (i, &byte) in bytes.iter().enumerate() { -# if byte == 32 { +# for (i, &item) in bytes.iter().enumerate() { +# if item == 32 { # return i; # } # } @@ -91,11 +92,12 @@ that it will still be valid in the future. Consider this: fn main() { let mut s = String::from("hello world"); - let word = first_word(&s); + let word = first_word(&s); // word will get the value 5. s.clear(); // This empties the String, making it equal to "". - // word is now totally invalid! There’s no more word here. + // word still has the value 5 here, but there's no more string that + // we could meaningfully use the value 5 with. word is now totally invalid! } ``` @@ -106,13 +108,13 @@ function. Its signature would have to look like this: fn second_word(s: &String) -> (usize, usize) { ``` -Now we’re tracking both a start _and_ and ending index. Even more chances for +Now we’re tracking both a start _and_ an ending index. Even more chances for things to go wrong. We now have three unrelated variable bindings floating around which need to be kept in sync. Luckily, Rust has a solution to this problem: string slices. -# String slices +## String slices A string slice looks like this: @@ -124,20 +126,21 @@ let world = &s[6..11]; ``` This looks just like taking a reference to the whole `String`, but with the -extra `[0..5]` bit. Instead of being a reference to the entire `String`, -it’s a reference to an internal position in the `String`, but it also keeps -track of the number of elements that it refers to as well. In other words, -it looks like this: +extra `[0..5]` bit. Instead of being a reference to the entire `String`, it’s a +reference to an internal position in the `String` and the number of elements +that it refers to. + +We can create slices with a range of `[starting_index..ending_index]`, but the +slice data structure actually stores the starting position and the length of the +slice. So in the case of `let world = &s[6..11];`, `world` would be a slice that +contains a pointer to the 6th byte of `s` and a length value of 5. + +In other words, it looks like this: DIAGRAM GOES HERE of s, hello, and world -Note that the internal position is specified through byte-offsets, not -characters. The offset to the first byte of a `String` is 0 and the -trailing number should point to the first byte that is _not_ included -in the slice. - -With Rust’s `..` syntax, if you want to start at zero, you can drop the zero. -In other words, these are equal: +With Rust’s `..` range syntax, if you want to start at the first index (zero), +you can drop the value before the `..`. In other words, these are equal: ```rust let s = String::from("hello"); @@ -147,7 +150,7 @@ let slice = &s[..2]; ``` By the same token, if your slice should include the last byte of the -`String`, you can drop the trailing number. In other words, these are +`String`, you can drop the trailing number. That means these are equal: ```rust @@ -155,8 +158,20 @@ let s = String::from("hello"); let len = s.len(); +let slice = &s[3..len]; +let slice = &s[3..]; +``` + +You can also drop both values to take a slice of the entire string. So these +are equal: + +```rust +let s = String::from("hello"); + +let len = s.len(); + let slice = &s[0..len]; -let slice = &s[0..]; +let slice = &s[..]; ``` With this in mind, let’s re-write `first_word()` to return a slice: @@ -165,8 +180,8 @@ With this in mind, let’s re-write `first_word()` to return a slice: fn first_word(s: &String) -> &str { let bytes = s.as_bytes(); - for (i, &byte) in bytes.iter().enumerate() { - if byte == 32 { + for (i, &item) in bytes.iter().enumerate() { + if item == 32 { return &s[0..i]; } } @@ -175,31 +190,22 @@ fn first_word(s: &String) -> &str { } ``` -Now, we have a single value, the `&str`. It contains both elements that we care -about: a reference to the starting point, and the number of elements. +Now we have a single value, the `&str`, pronounced "string slice". It stores +both elements that we care about: a reference to the starting point of the +slice and the number of elements in the slice. + This would also work for a `second_word()`: ```rust,ignore fn second_word(s: &String) -> &str { ``` -Same deal. We now have a straightforward API, that’s much harder to mess up. +We now have a straightforward API that’s much harder to mess up. But what about our error condition from before? Slices also fix that. Using the slice version of `first_word()` will throw an error: ```rust,ignore -# fn first_word(s: &String) -> &str { -# let bytes = s.as_bytes(); -# -# for (i, &byte) in bytes.iter().enumerate() { -# if byte == 32 { -# return &s[0..i]; -# } -# } -# -# &s[..] -# } fn main() { let mut s = String::from("hello world"); @@ -211,7 +217,7 @@ fn main() { Here’s the error: -```text +```bash 17:6 error: cannot borrow `s` as mutable because it is also borrowed as immutable [E0502] s.clear(); // Error! @@ -247,7 +253,7 @@ The type of `s` here is `&str`: It’s a slice, pointing to that specific point of the binary. This is also why string literals are immutable; `&str` is an immutable reference. -## String slices as arguments +### String slices as arguments Knowing that you can take slices of both literals and `String`s leads us to one more improvement on `first_word()`, and that’s its signature: @@ -272,8 +278,8 @@ with no loss of functionality: # fn first_word(s: &str) -> &str { # let bytes = s.as_bytes(); # -# for (i, &byte) in bytes.iter().enumerate() { -# if byte == 32 { +# for (i, &item) in bytes.iter().enumerate() { +# if item == 32 { # return &s[0..i]; # } # } @@ -281,17 +287,23 @@ with no loss of functionality: # &s[..] # } fn main() { - let s = String::from("hello world"); - let word = first_word(&s[..]); + let my_string = String::from("hello world"); - let s = "hello world"; - let word = first_word(&s[..]); + // first_word works on slices of `String`s + let word = first_word(&my_string[..]); - let word = first_word(s); // since literals are &strs, this works too! + let my_string_literal = "hello world"; + + // first_word works on slices of string literals + let word = first_word(&my_string_literal[..]); + + // since string literals *are* string slices already, + // this works too, without the slice syntax! + let word = first_word(my_string_literal); } ``` -# Other slices +## Other slices String slices, as you might imagine, are specific to strings. But there’s a more general slice type, too. Consider arrays: @@ -310,6 +322,6 @@ let slice = &a[1..3]; ``` This slice has the type `&[i32]`. It works the exact same way as string slices -do, with a reference to the first element, and a length. You’ll use this kind -of slice for all sorts of other collections. We’ll discuss these other slices -in detail when we talk about vectors, in Chapter 9.1. +do, by storing a reference to the first element and a length. You’ll use this +kind of slice for all sorts of other collections. We’ll discuss these in detail +when we talk about vectors in Chapter XX. diff --git a/src/ch05-00-structs.md b/src/ch05-00-structs.md new file mode 100644 index 000000000..f15f1bed5 --- /dev/null +++ b/src/ch05-00-structs.md @@ -0,0 +1,296 @@ +# Structs + +A `struct`, short for "structure", gives us the ability to name and package +together multiple related values that make up a meaningful group. If you come +from an object-oriented language, a `struct` is like an object's data +attributes. `struct` and `enum` (that we talked about in the last chapter) are +the building blocks you can use in Rust to create new types in your program's +domain in order to take full advantage of Rust's compile-time type checking. + +Let’s write a program which calculates the distance between two points. +We’ll start off with single variable bindings, and then refactor it to +use `struct`s instead. + +Let’s make a new project with Cargo: + +```bash +$ cargo new --bin points +$ cd points +``` + +Here’s a short program which calculates the distance between two points. Put +it into your `src/main.rs`: + +```rust +fn main() { + let x1 = 0.0; + let y1 = 5.0; + + let x2 = 12.0; + let y2 = 0.0; + + let answer = distance(x1, y1, x2, y2); + + println!("Point 1: ({}, {})", x1, y1); + println!("Point 2: ({}, {})", x2, y2); + println!("Distance: {}", answer); +} + +fn distance(x1: f64, y1: f64, x2: f64, y2: f64) -> f64 { + let x_squared = f64::powi(x2 - x1, 2); + let y_squared = f64::powi(y2 - y1, 2); + + f64::sqrt(x_squared + y_squared) +} +``` + +Let's try running this program with `cargo run`: + +```bash +$ cargo run + Compiling points v0.1.0 (file:///projects/points) + Running `target/debug/points` +Point 1: (0, 5) +Point 2: (12, 0) +Distance: 13 +``` + +Let's take a quick look at `distance()` before we move forward. To find the +distance between two points, we can use the Pythagorean Theorem. The theorem is +named after Pythagoras, who was the first person to mathematically prove this +formula. The details aren't that important; just know the theorem says that the +formula for the distance between two points is equal to: + +- squaring the distance between the points horizontally (the "x" direction) +- squaring the distance between the points vertically (the "y" direction) +- adding those together +- and taking the square root of that. + +So that's what we're implementing here. + +```rust,ignore +f64::powi(2.0, 3) +``` + +The double colon (`::`) here is a namespace operator. We haven’t talked about +modules and namespaces in depth yet, but you can think of the `powi()` function +as being scoped inside of another name. In this case, the name is `f64`, the +same as the type. The `powi()` function takes two arguments: the first is a +number, and the second is the power that it raises that number to. In this +case, the second number is an integer, hence the ‘i’ in its name. Similarly, +`sqrt()` is a function under the `f64` module, which takes the square root of +its argument. + +## Why `struct`s? + +Our little program is okay, but we can do better. The key to seeing this is in +the signature of `distance()`: + +```rust,ignore +fn distance(x1: f64, y1: f64, x2: f64, y2: f64) -> f64 { +``` + +The distance function is supposed to calculate the distance between two points. +But our distance function calculates some distance between four numbers. The +first two and last two arguments are related, but that’s not expressed anywhere +in our program itself. It would be nicer if we had a way to group `(x1, y1)` +and `(x2, y2)` together. + +We’ve already discussed one way to do that: tuples. Here’s a version of our +program which uses tuples: + +```rust +fn main() { + let p1 = (0.0, 5.0); + + let p2 = (12.0, 0.0); + + let answer = distance(p1, p2); + + println!("Point 1: {:?}", p1); + println!("Point 2: {:?}", p2); + println!("Distance: {}", answer); +} + +fn distance(p1: (f64, f64), p2: (f64, f64)) -> f64 { + let x_squared = f64::powi(p2.0 - p1.0, 2); + let y_squared = f64::powi(p2.1 - p1.1, 2); + + f64::sqrt(x_squared + y_squared) +} +``` + +This is a little better, for sure. Tuples let us add a little bit of structure. +We’re now passing two arguments, so that’s more clear. But it’s also worse: +tuples don’t give names to their elements, so our calculation has gotten more +confusing: + +```rust,ignore +p2.0 - p1.0 +p2.1 - p1.1 +``` + +When writing this example, your authors almost got it wrong themselves! Distance +is all about `x` and `y` points, but our code is talking about `0` and `1`. +This isn’t great. + +Enter `struct`s. We can transform our tuples into something with a name for the +whole as well as names for the parts: + +```rust,ignore +let p1 = (0.0, 5.0); + +struct Point { + x: f64, + y: f64, +} + +let p1 = Point { x: 0.0, y: 5.0 }; +``` + +Here we've defined a `struct` and given it the name `Point`. The parts inside +`{}` are defining the _fields_ of the struct. We can have as many or as few of +them as we'd like, and we give them a name and specify their type. Here we have +two fields named `x` and `y`, and they both hold `f64`s. + +We can access the field of a struct in the same way we access an element of +a tuple, except we use its name: + +```rust,ignore +let p1 = (0.0, 5.0); +let x = p1.0; + +struct Point { + x: f64, + y: f64, +} + +let p1 = Point { x: 0.0, y: 5.0 }; +let x = p1.x; +``` + +Let’s convert our program to use our `Point` `struct`. Here’s what it looks +like now: + +```rust +#[derive(Debug,Copy,Clone)] +struct Point { + x: f64, + y: f64, +} + +fn main() { + let p1 = Point { x: 0.0, y: 5.0}; + + let p2 = Point { x: 12.0, y: 0.0}; + + let answer = distance(p1, p2); + + println!("Point 1: {:?}", p1); + println!("Point 2: {:?}", p2); + println!("Distance: {}", answer); +} + +fn distance(p1: Point, p2: Point) -> f64 { + let x_squared = f64::powi(p2.x - p1.x, 2); + let y_squared = f64::powi(p2.y - p1.y, 2); + + f64::sqrt(x_squared + y_squared) +} +``` + +Our function signature for `distance()` now says exactly what we mean: it +calculates the distance between two `Point`s. And rather than `0` and `1`, +we’ve got back our `x` and `y`. This is a win for clarity. + +## Derived Traits + +There’s one other thing that’s a bit strange here, this stuff above the +`struct` declaration: + +```rust,ignore +#[derive(Debug,Copy,Clone)] +struct Point { +``` + +This is an annotation that tells the compiler our struct should get some +default behavior for the `Debug`, `Copy`, and `Clone` traits. We talked about +marking that types can be `Copy` and `Clone`-able in Chapter XX when we +discussed ownership. `Debug` is the trait that enables us to print out our +struct so that we can see its value while we are debugging our code. + +So far, we’ve been printing values using `{}` in a `println!` macro. If we try +that with a struct, however, by default, we'll get an error. Say we have the +following program: + +```rust,ignore +struct Point { + x: f64, + y: f64, +} + +fn main() { + let p1 = Point { x: 0.0, y: 5.0}; + println!("Point 1: {}", p1); +} +``` + +This code tries to print the `p1` point directly, which may seem innocuous. But +running it produces the following output: + +```bash +$ cargo run + Compiling points v0.1.0 (file:///projects/points) +error: the trait bound `Point: std::fmt::Display` is not satisfied [--explain E0277] + --> src/main.rs:8:29 +8 |> println!("Point 1: {}", p1); + |> ^^ +:2:27: 2:58: note: in this expansion of format_args! +:3:1: 3:54: note: in this expansion of print! (defined in ) +src/main.rs:8:5: 8:33: note: in this expansion of println! (defined in ) +note: `Point` cannot be formatted with the default formatter; try using `:?` instead if you are using a format string +note: required by `std::fmt::Display::fmt` +``` + +Whew! The core of the error is this part: *the trait bound `Point: +std::fmt::Display` is not satisfied*. `println!` can do many kinds of +formatting. By default, `{}` implements a kind of formatting known as +`Display`: output intended for direct end-user consumption. The primitive types +we’ve seen implement `Display`, as there’s only one way you’d show a `1` to a +user. But with structs, the output is less clear. Do you want commas or not? +What about the `{}`s? Should all the fields be shown? + +More complex types in the standard library and that are defined by the +programmer do not automatically implement `Display` formatting. Standard +library types implement `Debug` formatting, which is intended for the +programmer to see. The `#[derive(Debug)]` annotation lets us use a default +implementation of `Debug` formatting to easily get this ability for types we've +defined. To ask `println!` to use `Debug` formatting with our `Point`, we add +the annotation to derive the trait and include `:?` in the print string, like +this: + +```rust +#[derive(Debug)] +struct Point { + x: f64, + y: f64, +} + +fn main() { + let p1 = Point { x: 0.0, y: 5.0}; + println!("Point 1: {:?}", p1); +} +``` + +If you run this, it should print the values of each field in the `Point` struct +as desired: + +```bash +$ cargo run + Compiling points v0.1.0 (file:///projects/points) + Running `target/debug/points` +Point 1: Point { x: 0, y: 5 } +``` + +You’ll see this repeated later with other types. We’ll cover traits fully in +Chapter XX. diff --git a/src/ch05-02-method-syntax.md b/src/ch05-01-method-syntax.md similarity index 50% rename from src/ch05-02-method-syntax.md rename to src/ch05-01-method-syntax.md index ef4cb6ac6..925f70f3a 100644 --- a/src/ch05-02-method-syntax.md +++ b/src/ch05-01-method-syntax.md @@ -1,12 +1,12 @@ -# Method Syntax +## Method Syntax In the last section on ownership, we made several references to ‘methods’. Methods look like this: ```rust -let s1 = String::from("hello"); +let s1 = "hello"; -// call a method on our String +// call a method on s1 let s2 = s1.clone(); println!("{}", s1); @@ -27,13 +27,14 @@ h(g(f(x))); x.f().g().h(); ``` -The nested-functions version reads in reverse: we call `f()`, then `g()`, then -`h()`, but it reads as `h()`, then `g()`, then `f()`. +The nested-functions version reads in reverse: the program executes `f()`, then +`g()`, then `h()`, but we read it left-to-right as `h()`, then `g()`, then +`f()`. The method syntax is executed in the same order as we would read it. Before we get into the details, let’s talk about how to define your own methods. -## Defining methods +### Defining methods We can define methods with the `impl` keyword. `impl` is short for ‘implementation’. Doing so looks like this: @@ -63,80 +64,43 @@ assert_eq!(8.200609733428363, p1.distance(&p2)); Let’s break this down. First, we have our `Point` struct from earlier in the chapter. Next comes our first use of the `impl` keyword: -``` -# #[derive(Debug,Copy,Clone)] -# struct Point { -# x: f64, -# y: f64, -# } -# +```rust,ignore impl Point { -# fn distance(&self, other: &Point) -> f64 { -# let x_squared = f64::powi(other.x - self.x, 2); -# let y_squared = f64::powi(other.y - self.y, 2); -# -# f64::sqrt(x_squared + y_squared) -# } + // ... } -# -# let p1 = Point { x: 0.0, y: 0.0 }; -# let p2 = Point { x: 5.0, y: 6.5 }; -# -# assert_eq!(8.200609733428363, p1.distance(&p2)); ``` Everything we put inside of the curly braces will be methods implemented on -`Point`. +`Point`. Next is our definition: -``` -# #[derive(Debug,Copy,Clone)] -# struct Point { -# x: f64, -# y: f64, -# } -# -# impl Point { - fn distance(&self, other: &Point) -> f64 { -# let x_squared = f64::powi(other.x - self.x, 2); -# let y_squared = f64::powi(other.y - self.y, 2); -# -# f64::sqrt(x_squared + y_squared) - } -# } -# -# let p1 = Point { x: 0.0, y: 0.0 }; -# let p2 = Point { x: 5.0, y: 6.5 }; -# -# assert_eq!(8.200609733428363, p1.distance(&p2)); -``` - -Next is our definition. This looks very similar to our previous definition of -`distance()` as a function: - -```rust -# #[derive(Debug,Copy,Clone)] -# struct Point { -# x: f64, -# y: f64, -# } -fn distance(p1: Point, p2: Point) -> f64 { -# let x_squared = f64::powi(p2.x - p1.x, 2); -# let y_squared = f64::powi(p2.y - p1.y, 2); -# -# f64::sqrt(x_squared + y_squared) -# } +```rust,ignore +fn distance(&self, other: &Point) -> f64 { + // ... +} ``` Other than this, the rest of the example is familiar: an implementation of -`distance()`, and using the method to find an answer. +`distance()` and using the method to find an answer. -There are two differences. The first is in the first argument. Instead of a name -and a type, we have written `&self`. This is what distinguishes a method from a -function: using `self` inside of an `impl` block. Because we already know that -we are implementing this method on `Point`, we don’t need to write the type of -`self` out. However, we have written `&self`, not only `self`. This is because -we want to take our argument by reference rather than by ownership. In other -words, these two forms are the same: +Our definition of `distance()` here as a method looks very similar to our +previous definition of `distance()` as a function, but with two differences. +Here's the `distance()` function again: + +```rust,ignore +fn distance(p1: Point, p2: Point) -> f64 { + // ... +} +``` + +The first difference is in the first argument. Instead of a name and a type, we +have written `&self`. This is what distinguishes a method from a function: +using `self` inside of an `impl` block means we have a method. Because we +already know that we are implementing this method on `Point` because of the +surrounding `impl Point` block, we don’t need to write the type of `self` out. + +Note that we have written `&self`, not just `self`. This is because we want to +take a reference to our argument's value rather than taking ownership of it. In +other words, these two forms are the same: ```rust,ignore fn foo(self: &Point) @@ -152,34 +116,18 @@ fn foo(&mut self) // take self by mutable reference fn foo(self) // take self by ownership ``` -In this case, we only need a reference. We don’t plan on taking ownership, and -we don’t need to mutate either point. Taking by reference is by far the most -common form of method, followed by a mutable reference, and then occasionally -by ownership. +In this case, we only need a reference. We don’t need to mutate either `Point` +to get the distance between them, so we won't take a mutable reference to the +`Point` that we call the method on. Methods that take ownership of `self` are +rarely used. An example of a time to do that would be if we wanted to have a +method that would transform `self` into something else and prevent other code +from using the value of `self` after the transformation happens. -### Methods and automatic referencing +#### Methods and automatic referencing We’ve left out an important detail. It’s in this line of the example: -``` -# #[derive(Debug,Copy,Clone)] -# struct Point { -# x: f64, -# y: f64, -# } -# -# impl Point { -# fn distance(&self, other: &Point) -> f64 { -# let x_squared = f64::powi(other.x - self.x, 2); -# let y_squared = f64::powi(other.y - self.y, 2); -# -# f64::sqrt(x_squared + y_squared) -# } -# } -# -# let p1 = Point { x: 0.0, y: 0.0 }; -# let p2 = Point { x: 5.0, y: 6.5 }; -# +```rust,ignore assert_eq!(8.200609733428363, p1.distance(&p2)); ``` @@ -197,12 +145,12 @@ or `&mut`s to match the signature. In other words, these are the same: # x: f64, # y: f64, # } -# +# # impl Point { # fn distance(&self, other: &Point) -> f64 { # let x_squared = f64::powi(other.x - self.x, 2); # let y_squared = f64::powi(other.y - self.y, 2); -# +# # f64::sqrt(x_squared + y_squared) # } # } @@ -239,38 +187,3 @@ of a method whether the method is just reading (so needs `&self`), mutating (so `&mut self`), or consuming (so `self`). The fact that Rust makes borrowing implicit for method receivers is a big part of making ownership ergonomic in practice. - -## Methods can be called like functions - -Furthermore, if we have a method, we can also call it like a function: - -```rust -# #[derive(Debug,Copy,Clone)] -# struct Point { -# x: f64, -# y: f64, -# } -# -# impl Point { -# fn distance(&self, other: &Point) -> f64 { -# let x_squared = f64::powi(other.x - self.x, 2); -# let y_squared = f64::powi(other.y - self.y, 2); -# -# f64::sqrt(x_squared + y_squared) -# } -# } -# let p1 = Point { x: 0.0, y: 0.0 }; -# let p2 = Point { x: 5.0, y: 6.5 }; -let d1 = p1.distance(&p2); -let d2 = Point::distance(&p1, &p2); - -assert_eq!(d1, d2); -``` - -Instead of using `self.(`, we use `Point` and the namespace operator to call it -like a function instead. Because functions do not do the automatic referencing, -we must pass in `&p1` explicitly. - -While methods can be called like functions, functions cannot be called like -methods. If the first argument isn’t named `self`, it cannot be called like a -method. diff --git a/src/ch05-01-structs.md b/src/ch05-01-structs.md deleted file mode 100644 index 5d2bf3d52..000000000 --- a/src/ch05-01-structs.md +++ /dev/null @@ -1,228 +0,0 @@ -# Structs - -So far, all of the data types we’ve seen allow us to have a single value -at a time. `struct`s give us the ability to package up multiple values and -keep them in one related structure. - -Let’s write a program which calculates the distance between two points. -We’ll start off with single variable bindings, and then refactor it to -use `struct`s instead. - -Let’s make a new project with Cargo: - -```bash -$ cargo new --bin points -$ cd points -``` - -Here’s a short program which calculates the distance between two points. Put -it into your `src/main.rs`: - -```rust -fn main() { - let x1 = 0.0; - let y1 = 5.0; - - let x2 = 12.0; - let y2 = 0.0; - - let answer = distance(x1, y1, x2, y2); - - println!("Point 1: ({}, {})", x1, y1); - println!("Point 2: ({}, {})", x2, y2); - println!("Distance: {}", answer); -} - -fn distance(x1: f64, y1: f64, x2: f64, y2: f64) -> f64 { - let x_squared = f64::powi(x2 - x1, 2); - let y_squared = f64::powi(y2 - y1, 2); - - f64::sqrt(x_squared + y_squared) -} -``` - -Let's try running this program with `cargo run`: - -```bash -$ cargo run - Compiling points v0.1.0 (file:///projects/points) - Running `target/debug/points` -Point 1: (0, 5) -Point 2: (12, 0) -Distance: 13 -``` - -Let's take a quick look at `distance()` before we move forward: - -```rust -fn distance(x1: f64, y1: f64, x2: f64, y2: f64) -> f64 { - let x_squared = f64::powi(x2 - x1, 2); - let y_squared = f64::powi(y2 - y1, 2); - - f64::sqrt(x_squared + y_squared) -} -``` - -To find the distance between two points, we can use the Pythagorean Theorem. -The theorem is named after Pythagoras, who was the first person to mathematically -prove this formula. The details aren't that important, to be honest. There's a few -things that we haven't discussed yet, though. - -```rust,ignore -f64::powi(2.0, 3) -``` - -The double colon (`::`) here is a namespace operator. We haven’t talked about -modules yet, but you can think of the `powi()` function as being scoped inside -of another name. In this case, the name is `f64`, the same as the type. The -`powi()` function takes two arguments: the first is a number, and the second is -the power that it raises that number to. In this case, the second number is an -integer, hence the ‘i’ in its name. Similarly, `sqrt()` is a function under the -`f64` module, which takes the square root of its argument. - -## Why `struct`s? - -Our little program is okay, but we can do better. The key is in the signature -of `distance()`: - -```rust,ignore -fn distance(x1: f64, y1: f64, x2: f64, y2: f64) -> f64 { -``` - -The distance function is supposed to calculate the distance between two points. -But our distance function calculates some distance between four numbers. The -first two and last two arguments are related, but that’s not expressed anywhere -in our program itself. We need a way to group `(x1, y1)` and `(x2, y2)` -together. - -We’ve already discussed one way to do that: tuples. Here’s a version of our program -which uses tuples: - -```rust -fn main() { - let p1 = (0.0, 5.0); - - let p2 = (12.0, 0.0); - - let answer = distance(p1, p2); - - println!("Point 1: {:?}", p1); - println!("Point 2: {:?}", p2); - println!("Distance: {}", answer); -} - -fn distance(p1: (f64, f64), p2: (f64, f64)) -> f64 { - let x_squared = f64::powi(p2.0 - p1.0, 2); - let y_squared = f64::powi(p2.1 - p1.1, 2); - - f64::sqrt(x_squared + y_squared) -} -``` - -This is a little better, for sure. Tuples let us add a little bit of structure. -We’re now passing two arguments, so that’s more clear. But it’s also worse. -Tuples don’t give names to their elements, and so our calculation has gotten -much more confusing: - -```rust,ignore -p2.0 - p1.0 -p2.1 - p1.1 -``` - -When writing this example, your authors almost got it wrong themselves! Distance -is all about `x` and `y` points, but now it’s all about `0` and `1`. This isn’t -great. - -Enter `struct`s. We can transform our tuples into something with a name: - -```rust,ignore -let p1 = (0.0, 5.0); - -struct Point { - x: f64, - y: f64, -} - -let p1 = Point { x: 0.0, y: 5.0 }; -``` - -Here’s what declaring a `struct` looks like: - -```text -struct NAME { - NAME: TYPE, -} -``` - -The `NAME: TYPE` bit is called a ‘field’, and we can have as many or as few of -them as you’d like. If you have none of them, drop the `{}`s: - -```rust -struct Foo; -``` - -`struct`s with no fields are called ‘unit structs’, and are used in certain -advanced situations. We will just ignore them for now. - -You can access the field of a struct in the same way you access an element of -a tuple, except you use its name: - -```rust,ignore -let p1 = (0.0, 5.0); -let x = p1.0; - -struct Point { - x: f64, - y: f64, -} - -let p1 = Point { x: 0.0, y: 5.0 }; -let x = p1.x; -``` - -Let’s convert our program to use our `Point` `struct`. Here’s what it looks -like now: - -```rust -#[derive(Debug,Copy,Clone)] -struct Point { - x: f64, - y: f64, -} - -fn main() { - let p1 = Point { x: 0.0, y: 5.0}; - - let p2 = Point { x: 12.0, y: 0.0}; - - let answer = distance(p1, p2); - - println!("Point 1: {:?}", p1); - println!("Point 2: {:?}", p2); - println!("Distance: {}", answer); -} - -fn distance(p1: Point, p2: Point) -> f64 { - let x_squared = f64::powi(p2.x - p1.x, 2); - let y_squared = f64::powi(p2.y - p1.y, 2); - - f64::sqrt(x_squared + y_squared) -} -``` - -Our function signature for `distance()` now says exactly what we mean: it -calculates the distance between two `Point`s. And rather than `0` and `1`, -we’ve got back our `x` and `y`. This is a win for clarity. - -There’s one other thing that’s a bit strange here, this annotation on our -`struct` declaration: - -```rust,ignore -#[derive(Debug,Copy,Clone)] -struct Point { -``` - -We haven’t yet talked about traits, but we did talk about `Debug` when we -discussed arrays. This `derive` attribute allows us to tweak the behavior of -our `Point`. In this case, we are opting into copy semantics, and everything -that implements `Copy` must implement `Clone`. diff --git a/src/ch06-01-enums.md b/src/ch06-00-enums.md similarity index 60% rename from src/ch06-01-enums.md rename to src/ch06-00-enums.md index 0e669d170..c229ae7ce 100644 --- a/src/ch06-01-enums.md +++ b/src/ch06-00-enums.md @@ -1,12 +1,13 @@ # Enums -Next, let’s look at a feature of Rust that’s similar to structs, but also -different. Enumerations, or ‘enums’ as they’re more commonly referred to, -are an extremely powerful feature of Rust. Enums are a feature that are in many -languages, but what they can do is different per-language. Rust’s enums are -most similar to enums in functional languages. +Next, let’s look at *enumerations*, which allow you to define a type by +enumerating its possible values. Commonly called "enums", these unlock a lot of +power in Rust when combined with pattern matching. Enums are a feature that are +in many languages, but what they can do is different per-language. Rust’s enums +are most similar to "algebraic data types" in functional languages like F#, +OCaml, or Haskell. -Here’s an example of an enum: +Here’s an example of an enum definition: ```rust enum IpAddrKind { @@ -16,10 +17,10 @@ enum IpAddrKind { ``` This enum represents the kind of an IP address. There are two major standards -used for IP addresses: version four, and version six. Any IP address can be either -a version four address, or a version six address. But it cannot be both kinds at -the same time. This is where enums get their name: they allow us to enumerate all -of the possible kinds that our value can have. +used for IP addresses: version four and version six. Any IP address can be +either a version four address or a version six address, but it cannot be both +kinds at the same time. This is where enums get their name: they allow us to +enumerate all of the possible kinds that our value can have. We can create values of `IpAddrKind` like this: @@ -28,7 +29,7 @@ We can create values of `IpAddrKind` like this: # V4, # V6, # } - +# let four = IpAddrKind::V4; let six = IpAddrKind::V6; ``` @@ -37,7 +38,7 @@ Note that the variants of the enum are namespaced under its name, and we use the double colon to separate the two. Enums have more tricks up their sleeves, however. Thinking more about our IP -address type, we don’t have a way to store the actual data of the IP address, +address type, we don’t have a way to store the actual data of the IP address; we only know what kind it is. Given that you just learned about structs, you might tackle this problem like this: @@ -64,8 +65,8 @@ let loopback = IpAddr { ``` We’ve used a struct to bundle the two values together: now we keep the kind -with the value itself. This design isn’t bad, exactly, but it wouldn’t be -considered idiomatic Rust. We can represent the same thing with just an enum: +with the value itself. We can represent the same thing in a different way with +just an enum: ```rust enum IpAddr { @@ -82,12 +83,12 @@ We can attach data to each variant of the enum directly. No need for an extra struct. But beyond that, this approach is better than using a struct alongside our enum because we can attach different kinds of data to each variant. Imagine that instead of a `String`, we would prefer to store a `V4` as its four -individual components, while leaving the `V6` variant as a `String`. With our +individual components while leaving the `V6` variant as a `String`. With our struct, we’d be stuck. But enums deal with this case with ease: ```rust enum IpAddr { - V4(u32, u32, u32, u32), + V4(u8, u8, u8, u8), V6(String), } @@ -97,8 +98,8 @@ let loopback = IpAddr::V6(String::from("::1")); ``` You can put any kind of data inside of an enum variant, including another enum! -The `IpAddr` enum is [in the standard library][IpAddr], but it embeds two different -structs inside of its variants: +The `IpAddr` enum is [in the standard library][IpAddr], but it embeds two +different structs inside of its variants: ```rust struct Ipv4Addr { @@ -133,16 +134,21 @@ enum Message { * `Write` includes a single `String`. * `ChangeColor` includes three `i32`s. -We haven’t talked a lot about how to access the data inside an enum variant, -however. To do that, let’s move on to some new Rust syntax that’s especially -useful with enums: `match`. - - - - - - - - +This might seem overwhelming, but another way to look at the different enum +possibilities is that they are just like different kinds of struct definitions +that you already know, except without the `struct` keyword and they are grouped +together under the `Message` type. These structs could hold the same data that +these enum variants hold: +``` +struct QuitMessage; // unit struct +struct MoveMessage { + x: i32, + y: i32, +} +struct WriteMessage(String); // tuple struct +struct ChangeColorMessage(i32, i32, i32); // tuple struct +``` +Let's look at another enum in the standard library that is very common and +useful: `Option`. diff --git a/src/ch06-02-option.md b/src/ch06-01-option.md similarity index 66% rename from src/ch06-02-option.md rename to src/ch06-01-option.md index f9b0ce0ba..9e84b0d16 100644 --- a/src/ch06-02-option.md +++ b/src/ch06-01-option.md @@ -1,9 +1,9 @@ -# Option +## Option -Now that we have a handle on enums, let's combine them with a feature that we -talked a little bit about in the previous chapter: generics. +Now that we have had an introduction to enums, let's combine them with a +feature that we talked a little bit about in the previous chapter: generics. -Programming language design is often though of as which features you include, +Programming language design is often thought of as which features you include, but it's also about which features you leave out. Rust does not have a feature that is in many other languages: 'null'. In languages with this feature, variables can have two states: null or not-null. @@ -18,7 +18,7 @@ The inventor of this concept has this to say: > implement. This has led to innumerable errors, vulnerabilities, and system > crashes, which have probably caused a billion dollars of pain and damage in > the last forty years. -> +> > - Tony Hoare "Null References: The Billion Dollar Mistake" The problem with null values is twofold: first, a value can be null or not, at @@ -30,8 +30,8 @@ Even with these problems, the concept that null is trying to express is still a useful one: this is a value which is currently invalid or not present for some reason. The problem isn't with the concept itself, but with the particular implementation. As such, Rust does not have the concept of null, but we do have -a type which can encode the concept of a value being present. We call this type -`Option`, and it looks like this: +an enum which can encode the concept of a value being present or not present. We +call this enum `Option`, and it looks like this: ```rust enum Option { @@ -40,7 +40,7 @@ enum Option { } ``` -This type is [provided by the standard library][option], and is so useful that +This enum is [provided by the standard library][option], and is so useful that it's even in the prelude; you don't need to import it explicitly. Furthermore, so are its variants: you can say `Some` and `None` directly, without prefixing them with `Option::`. @@ -67,8 +67,8 @@ In short, because `Option` and `T` are different types. That's a bit too short though. Here's an example: ```rust,ignore -let x = 5; -let y = Some(5); +let x: i8 = 5; +let y: Option = Some(5); let sum = x + y; ``` @@ -76,8 +76,8 @@ let sum = x + y; This will not compile. We get an error message like this: ```text -error: the trait `core::ops::Add>` is not implemented -for the type `_` [E0277] +error: the trait bound `i8: std::ops::Add>` is not +satisfied [E0277] let sum = x + y; ^~~~~ @@ -85,17 +85,26 @@ let sum = x + y; Intense! What this error message is trying to say is that Rust does not understand how to add an `Option` and a `T`. They're different types! This -shows one of the big advantages of an `Option` type: if you have a type that +shows one of the big advantages of an `Option`: if you have a value that may or may not exist, you have to deal with that fact before you can assume it exists. In other words, you have to convert an `Option` to a `T` before you can do `T` stuff with it. This helps catch one of the most common issues with -null, generally: assuming that something isn't null, when it actually is. +null, generally: assuming that something isn't null when it actually is. -So, how _do_ you get a `T` from an `Option`? The option type has a large -number of methods that you can check out in [its documentation], and becoming -familiar with them will be extremely useful in your journey with Rust. +This is pretty powerful: in order to have a value that can possibly be null, +you have to explicitly opt in by making the type of that value an `Option`. +Then, when you use that value, you are required to explicitly handle the case +when the value is null. Everywhere that a value has a type that isn't an +`Option`, you *can* safely assume that the value isn't null. This was a +deliberate design decision for Rust to limit null's pervasiveness and increase +the safety of Rust code. + +So, how _do_ you get a `T` from an `Option`? The `Option` enum has a +large number of methods that you can check out in [its documentation], and +becoming familiar with them will be extremely useful in your journey with Rust. [its documentation]: ../std/option/enum.Option.html But we want a deeper understanding than that. If we didn't have those methods -defined for us already, what would we do? For that, we need a new feature: `match`. +defined for us already, what would we do? And more generally, how do we get +the inner values out of any enum variant? We need a new feature: `match`. diff --git a/src/ch06-02-match.md b/src/ch06-02-match.md new file mode 100644 index 000000000..6e7787fb4 --- /dev/null +++ b/src/ch06-02-match.md @@ -0,0 +1,268 @@ +## Match + +Rust has an extremely powerful control-flow operator: `match`. It allows us to +compare a value against a series of patterns and then execute code based on +how they compare. + +Think of a `match` expression kind of like a coin sorting machine. Coins slide +down a track that has variously sized holes along it, and each coin falls +through the first hole it encounters that it fits into. In the same way, values +go through each pattern in a `match`, and for the first pattern that the value +"fits", the value will fall into the associated code block to be used during +execution. + +Since we're already talking about coins, let's use them for an example using +`match`! We can write a function that can take an unknown American coin and, in +a similar way as the coin counting machine, determine which coin it is and +return its value in cents: + +```rust +enum Coin { + Penny, + Nickel, + Dime, + Quarter, +} + +fn value_in_cents(coin: Coin) -> i32 { + match coin { + Coin::Penny => 1, + Coin::Nickel => 5, + Coin::Dime => 10, + Coin::Quarter => 25, + } +} +``` + +Let's break down the `match`! At a high-level, using `match` looks like this: + +```text +match expression { + pattern => code, +} +``` + +First, we have the `match` keyword. Next, we have an expression. This feels +very similar to an expression used with `if`, but there's a big difference: +with `if`, the condition needs to return a boolean value. Here, it can be any +type. + +Next, we have a "match arm". That's the part that looks like `pattern => +code,`. We can have as many arms as we need to: our `match` above has four +arms. An arm has two parts: a pattern and some code. When the `match` +expression executes, it compares the resulting value against the pattern of +each arm, in order. If a pattern matches the value, the code associated +with that pattern is executed. If that pattern doesn't match the value, +execution continues to the next arm, much like a coin sorting machine. + +The code associated with each arm is an expression, and the resulting value of +the code with the matching arm that gets executed is the value that gets +returned for the entire `match` expression. + +Curly braces typically aren't used if the match arm code is short, as it is in +the above example where each arm just returns a value. If we wanted to run +multiple lines of code in a match arm, we can use curly braces. This code would +print out "Lucky penny!" every time the method was called with a `Coin::Penny`, +but would still return the last value of the block, `1`: + +```rust +# enum Coin { +# Penny, +# Nickel, +# Dime, +# Quarter, +# } +# +fn value_in_cents(coin: Coin) -> i32 { + match coin { + Coin::Penny => { + println!("Lucky penny!"); + 1 + }, + Coin::Nickel => 5, + Coin::Dime => 10, + Coin::Quarter => 25, + } +} +``` + +Another useful feature of match arms is that they can create bindings to parts +of the values that match the pattern. From 1999 through 2008, the U.S. printed +quarters with different designs for each of the 50 states on one side. The other +coins did not get state designs, so only quarters have this extra attribute. We +can add this information to our `enum` by changing the `Quarter` variant to have +a `State` value: + +```rust +enum UsState { + Alabama, + Alaska, + // ... etc +} + +enum Coin { + Penny, + Nickel, + Dime, + Quarter(UsState), +} +``` + +Let's imagine that a friend of ours is trying to collect all 50 state quarters. +While we sort our loose change by coin type in order to count it, we're going +to call out the name of the state so that if it's one our friend doesn't have +yet, they can add it to their collection. + +In the match statement to do this, the quarter case now has a binding, `state`, +that contains the value of the state of that quarter. The binding will only get +created if the coin matches the `Quarter` pattern. Then we can use the binding +in the code for that arm: + +```rust +# #[derive(Debug)] +# enum UsState { +# Alabama, +# Alaska, +# } +# +# enum Coin { +# Penny, +# Nickel, +# Dime, +# Quarter(UsState), +# } +# +fn value_in_cents(coin: Coin) -> i32 { + match coin { + Coin::Penny => 1, + Coin::Nickel => 5, + Coin::Dime => 10, + Coin::Quarter(state) => { + println!("State quarter from {:?}!", state); + 25 + }, + } +} +``` + +If we were to call `value_in_cents(Coin::Quarter(UsState::Alaska))`, `coin` will +be `Coin::Quarter(UsState::Alaska)`. When we compare that value with each of the +match arms, none of them match until we reach `Coin::Quarter(state)`. At that +point, the binding for `state` will be the value `UsState::Alaska`. We can then +use that binding in the `println!`, thus getting the inner state value out of +the `Coin` enum variant for `Quarter`. + +Remember the `Option` type from the previous section, and that we wanted to +be able to get the inner `T` value out of the `Some` case? This will be very +similar! Instead of coins, we will be comparing to other patterns, but the way +that the `match` expression works remains the same as a coin sorting machine in +the way that we look for the first pattern that fits the value. + +Let's say that we want to write a function that takes an `Option`, and +if there's a value inside, add one to it. If there isn't a value inside, we +want to return the `None` value and not attempt to add. + +This function is very easy to write, thanks to `match`. It looks like this: + +```rust +fn plus_one(x: Option) -> Option { + match x { + None => None, + Some(i) => Some(i + 1), + } +} + +let five = Some(5); +let six = plus_one(five); +let none = plus_one(None); +``` + +Let's examine the first execution of `plus_one()` in more detail. In the above +example, `x` will be `Some(5)`. Let's compare that against each arm: + +```text +None => None, +``` + +Does `Some(5)` match `None`? No, it's the wrong variant. So let's continue. + +```text +Some(i) => Some(i + 1), +``` + +Does `Some(5)` match `Some(i)`? Why yes it does! We have the same variant. The +`i` binds to the value inside of the `Some`, so `i` has the value `5`. Then we +execute the code in that match arm: take `i`, which is `5`, add one to it, and +create a new `Some` value with our total inside. + +Now let's consider the second call of `plus_one()`. In this case, `x` is +`None`. We enter the `match`, and compare to the first arm: + +```text +None => None, +``` + +Does `None` match `None`? Yup! There's no value to add to. So we stop and +return the `None` value that is on the right side of the `=>`. We don't +check any other arms since we found one that matched. + +Combining `match` and enums together is extremely powerful. You'll see this +pattern a lot in Rust code: `match` against an enum, bind to the data +inside, and then execute code based on it. It's a bit tricky at first, but +once you get used to it, you'll wish you had it in languages that don't support +it. It's consistently a user favorite. + +### Matches are exhaustive + +There's one other aspect of `match` we didn't talk about. Consider this version +of `plus_one()`: + +```rust,ignore +fn plus_one(x: Option) -> Option { + match x { + Some(i) => Some(i + 1), + } +} +``` + +A bug! We didn't handle the `None` case. Luckily, it's a bug Rust knows how to +catch. If we try to compile this code, we'll get an error: + +```text +error: non-exhaustive patterns: `None` not covered [E0004] +match x { + Some(i) => Some(i + 1), +} +``` + +Rust knows that we did not cover every possible option, and even knows which +pattern we forgot! This is referred to as being "exhaustive": we must exhaust +every last option possible in order to be valid. Especially in the case of +`Option`, when Rust prevents us from forgetting to explicitly handle the +`None` case, it protects us from assuming that we have a value when we might +have null and thus making the billion-dollar mistake we discussed in the +previous section. + +### The _ placeholder + +What if we don't care about all of the possible values, though? Especially when +there are a lot of possible values for a type: a `u8` can have valid values of +zero through 255-- if we only care about 1, 3, 5, and 7, does this mean we must +list out 0, 2, 4, 6, 8, 9, all the way up through 255? Thankfully, no! We can +use a special pattern, `_`: + +```rust +let some_u8_value = 0u8; +match some_u8_value { + 1 => println!("one"), + 3 => println!("three"), + 5 => println!("five"), + 7 => println!("seven"), + _ => (), +} +``` + +The `_` pattern will match all the other cases, and `()` will do nothing, it's +the unit value. This way, we don't have to list individual match arms for all +the other possible values in order to say that we want to do nothing for all of +those-- the `_` is a placeholder for any value. diff --git a/src/ch06-04-if-let.md b/src/ch06-03-if-let.md similarity index 86% rename from src/ch06-04-if-let.md rename to src/ch06-03-if-let.md index 25c5b11e8..ee19884db 100644 --- a/src/ch06-04-if-let.md +++ b/src/ch06-03-if-let.md @@ -1,4 +1,4 @@ -# if let +## if let There's one more advanced control flow structure we haven't discussed: `if let`. Imagine we're in a situation like this: @@ -16,9 +16,9 @@ match some_option { We care about the `Some` case, but don't want to do anything with the `None` case. With an `Option`, this isn't _too_ bad, but with a more complex enum, adding `_ => {}` after processing just one variant doesn't feel great. We have -this boilerplate arm, and we have an extra level of indentation: the code that -does something with `x` is indented twice, rather than just once. We really want -a construct that says "Do something with this one case, I don't care about the +this boilerplate arm and an extra level of indentation (the code that +does something with `x` is indented twice, rather than just once). We really want +a construct that says "Do something with this one case; I don't care about the others." Enter `if let`: @@ -63,4 +63,4 @@ match expression { ``` In other words, it's the high-level construct we were originally looking for: -do something with a single pattern. +do something special with only one pattern. diff --git a/src/ch06-03-match.md b/src/ch06-03-match.md deleted file mode 100644 index 1e4696d88..000000000 --- a/src/ch06-03-match.md +++ /dev/null @@ -1,182 +0,0 @@ -# Match - -Rust has an extremely powerful control-flow operator: `match`. It allows us to -compare a value against a series of patterns, and then execute code based on -how they compare. Remember the `Option` type from the previous section? -Let's say that we want to write a function that takes an `Option`, and -if there's a value inside, add one to it. - -This function is very easy to write, thanks to `match`. It looks like this: - -```rust -fn plus_one(x: Option) -> Option { - match x { - None => None, - Some(i) => Some(i + 1), - } -} - -let five = Some(5); -let six = plus_one(five); -let none = plus_one(None); -``` - -Let's break down the `match`! At a high-level, the `match` expression looks -like this: - -```text -match condition { - pattern => code, -} -``` - -First, we have the `match` keyword. Next, we have a condition. This feels very -similar to an `if` expression, but there's a big difference: with `if`, the -condition needs to be a boolean. Here, it can be any type. - -Next, we have a "match arm". That's the part that looks like `pattern => -code,`. We can have as many arms as we need to: our `match` above has two -arms. An arm has two parts: a pattern, and some code. When the `match` -expression executes, it compares the condition against the pattern of each arm, -in turn. If the pattern matches the condition, the associated code is executed, -and the rest of the patterns are not checked. If it doesn't match, execution -continues to the next arm. - -Let's examine the first execution of `plus_one()` in more detail. In the above -example, `x` will be `Some(5)`. Let's compare that against each arm: - -```text -None => None, -``` - -Does `Some(5)` match `None`? No, it's the wrong variant. So let's continue. - -```text -Some(i) => Some(i + 1), -``` - -Does `Some(5)` match `Some(i)`? Why yes it does! We have the same variant. But -what about `i`? In a pattern like this, we can declare new bindings, similarly -to what we did with `let`. So in this case, the code part of the match arm will -have a binding, `i`, which corresponds to the `5`. - -With this arm, the code portion is `Some(i + 1)`. So we do exactly that: we -take `i`, which is `5`, add one to it, and create a new `Some` value with our -sum inside. - -Because `match` is an expression, the value of the overall expression becomes -the value of the arm that executed. So the value of this `match` expression -will be `Some(6)`. And since our `match` is the only expression in the -function, the value of the `match` will be the value of the function, and so -`Some(6)` is our return value as well, which is exactly what we were shooting -for. - -Now let's consider the second call. In this case, `x` is `None`. We enter the -`match`, and compare to the first arm: - -```text -None => None, -``` - -Does `None` match `None`? Yup! And so we return `None`. There's no value to add -to. - -Combining `match` and enums together is extremely powerful. You'll see this -pattern a lot in Rust code: `match` against an enum, binding to the data -inside, and then executing code based on it. It's a bit tricky at first, but -once you get used to it, you'll wish you had it in languages that don't support -it. It's consistently a user favorite. - -## Matches are exhaustive - -There's one other aspect of `match` we didn't talk about. Consider this version -of `plus_one()`: - -```rust,ignore -fn plus_one(x: Option) -> Option { - match x { - Some(i) => Some(i + 1), - } -} -``` - -A bug! We didn't handle the `None` case. Luckily, it's a bug Rust knows how to catch. -If we try to compile this code, we'll get an error: - -```text -error: non-exhaustive patterns: `None` not covered [E0004] -match x { - Some(i) => Some(i + 1), -} -``` - -Rust knows that we did not cover every possible option, and even knows which -pattern we forgot! This is referred to as being "exhaustive", we must exhaust -every last option possible in order to be valid! - -This analysis isn't perfect, however. This will also error: - -```rust,ignore -# let some_u8_value = 0u8; -match some_u8_value { - 0 => println!("zero"), - 1 => println!("one"), - 2 => println!("two"), - 3 => println!("three"), - 4 => println!("four"), - 5 => println!("five"), - 6 => println!("six"), - 7 => println!("seven"), - // We won't write out all of the arms here, but imagine that there are more - // arms corresponding to the rest of the numbers. - 254 => println!("two-hundred and fifty-four"), - 255 => println!("two-hundred and fifty-five"), -} -``` - -Even though a `u8` can only have valid values of zero through 255, Rust isn't -quite smart enough to understand we've covered all the cases. In order to fix -this, we can use a special pattern, `_`: - -```rust -# let some_u8_value = 0u8; -match some_u8_value { - 0 => println!("zero"), - 1 => println!("one"), - 2 => println!("two"), - 3 => println!("three"), - 4 => println!("four"), - 5 => println!("five"), - 6 => println!("six"), - 7 => println!("seven"), - // ... - 254 => println!("two-hundred and fifty-four"), - 255 => println!("two-hundred and fifty-five"), - _ => panic!("can't ever happen"), -} -``` - -The `_` pattern matches anything at all, and so with it as the final pattern, -Rust can understand that we have all our bases covered. It's not only used for -this sort of exhaustiveness issue, though. It's useful any time we don't want to -deal with a number of cases. Consider this scenario: if we wanted to print out -something one one, three, five, and seven: - -```rust -# let some_u8_value = 0u8; -match some_u8_value { - 1 => println!("one"), - 3 => println!("three"), - 5 => println!("five"), - 7 => println!("seven"), - _ => (), -} -``` - -The `_` pattern will match all the other cases, and `()` will do nothing, it's -the unit value. - -## More about patterns - -As we've just seen, patterns are powerful, yet complex. Let's take a whole -section to cover all of the things that they can do.