doc.rust-lang.org
Reading a File Now we’ll add functionality to read the file specified in the file_path argument. First, we need a sample file to test it with: We’ll use a file with a small amount of text over multiple lines with some repeated words. Listin...
The Rust Programming Language
Book
2024-02-01
~1 min read
doc.rust-lang.org
Accepting Command Line Arguments Let’s create a new project with, as always, cargo new. We’ll call our project minigrep to distinguish it from the grep tool that you might already have on your system: $ cargo new minigrep Created binary (ap...
The Rust Programming Language
Book
2024-02-01
~4 min read
doc.rust-lang.org
An I/O Project: Building a Command Line Program This chapter is a recap of the many skills you’ve learned so far and an exploration of a few more standard library features. We’ll build a command line tool that interacts with file and comman...
The Rust Programming Language
Book
2024-02-01
~1 min read
doc.rust-lang.org
Test Organization As mentioned at the start of the chapter, testing is a complex discipline, and different people use different terminology and organization. The Rust community thinks about tests in terms of two main categories: unit tests...
The Rust Programming Language
Book
2024-02-01
~8 min read
doc.rust-lang.org
Controlling How Tests Are Run Just as cargo run compiles your code and then runs the resultant binary, cargo test compiles your code in test mode and runs the resultant test binary. The default behavior of the binary produced by cargo test...
The Rust Programming Language
Book
2024-02-01
~5 min read
doc.rust-lang.org
How to Write Tests Tests are Rust functions that verify that the non-test code is functioning in the expected manner. The bodies of test functions typically perform these three actions: • Set up any needed data or state. • Run the code you...
The Rust Programming Language
Book
2024-02-01
~15 min read
doc.rust-lang.org
Writing Automated Tests In his 1972 essay “The Humble Programmer,” Edsger W. Dijkstra said that “program testing can be a very effective way to show the presence of bugs, but it is hopelessly inadequate for showing their absence.” That does...
The Rust Programming Language
Book
2024-02-01
~1 min read
doc.rust-lang.org
Validating References with Lifetimes Lifetimes are another kind of generic that we’ve already been using. Rather than ensuring that a type has the behavior we want, lifetimes ensure that references are valid as long as we need them to be. O...
The Rust Programming Language
Book
2024-02-01
~21 min read
doc.rust-lang.org
Defining Shared Behavior with Traits A trait defines the functionality a particular type has and can share with other types. We can use traits to define shared behavior in an abstract way. We can use trait bounds to specify that a generic t...
The Rust Programming Language
Book
2024-02-01
~12 min read
doc.rust-lang.org
Generic Data Types We use generics to create definitions for items like function signatures or structs, which we can then use with many different concrete data types. Let’s first look at how to define functions, structs, enums, and methods...
The Rust Programming Language
Book
2024-02-01
~10 min read
doc.rust-lang.org
Generic Types, Traits, and Lifetimes Every programming language has tools for effectively handling the duplication of concepts. In Rust, one such tool is generics: abstract stand-ins for concrete types or other properties. We can express th...
The Rust Programming Language
Book
2024-02-01
~4 min read
doc.rust-lang.org
To panic! or Not to panic! So, how do you decide when you should call panic! and when you should return Result? When code panics, there’s no way to recover. You could call panic! for any error situation, whether there’s a possible way to re...
The Rust Programming Language
Book
2024-02-01
~10 min read
doc.rust-lang.org
Recoverable Errors with Result Most errors aren’t serious enough to require the program to stop entirely. Sometimes when a function fails, it’s for a reason that you can easily interpret and respond to. For example, if you try to open a fil...
The Rust Programming Language
Book
2024-02-01
~18 min read
doc.rust-lang.org
Unrecoverable Errors with panic! Sometimes bad things happen in your code, and there’s nothing you can do about it. In these cases, Rust has the panic! macro. There are two ways to cause a panic in practice: by taking an action that causes...
The Rust Programming Language
Book
2024-02-01
~5 min read
doc.rust-lang.org
Error Handling Errors are a fact of life in software, so Rust has a number of features for handling situations in which something goes wrong. In many cases, Rust requires you to acknowledge the possibility of an error and take some action b...
The Rust Programming Language
Book
2024-02-01
~1 min read
doc.rust-lang.org
Storing Keys with Associated Values in Hash Maps The last of our common collections is the hash map. The type HashMap<K, V> stores a mapping of keys of type K to values of type V using a hashing function, which determines how it places thes...
The Rust Programming Language
Book
2024-02-01
~8 min read
doc.rust-lang.org
Storing UTF-8 Encoded Text with Strings We talked about strings in Chapter 4, but we’ll look at them in more depth now. New Rustaceans commonly get stuck on strings for a combination of three reasons: Rust’s propensity for exposing possible...
The Rust Programming Language
Book
2024-02-01
~11 min read
doc.rust-lang.org
Storing Lists of Values with Vectors The first collection type we’ll look at is Vec<T>, also known as a vector. Vectors allow you to store more than one value in a single data structure that puts all the values next to each other in memory....
The Rust Programming Language
Book
2024-02-01
~8 min read
doc.rust-lang.org
Common Collections Rust’s standard library includes a number of very useful data structures called collections. Most other data types represent one specific value, but collections can contain multiple values. Unlike the built-in array and t...
The Rust Programming Language
Book
2024-02-01
~1 min read
doc.rust-lang.org
Separating Modules into Different Files So far, all the examples in this chapter defined multiple modules in one file. When modules get large, you might want to move their definitions to a separate file to make the code easier to navigate....
The Rust Programming Language
Book
2024-02-01
~4 min read