Week 12: Building a Real Rust Program
panic!
, which immediately stops the program.Result<T, E>
enum, which lets a function return either a success value (Ok
) or an error value (Err
).Result
and Option
enumsResult
and Option
are the most important enums for handling potential failures in Rust.Option<T>
is used when a value could be something (Some(T)
) or nothing (None
). Think of it as preventing null
pointer errors.Result<T, E>
is for operations that can fail. It returns Ok(T)
on success or Err(E)
on failure.match
expression to handle every possible variant of these enums.?
operator for cleaner error handlingmatch
to handle every error can be verbose. The question mark (?
) operator is a powerful shortcut for propagating errors.Result
, ?
does two things:
Result
is Ok
, it unwraps the value and continues.Result
is Err
, it immediately returns the Err
from the whole function.Result
or Option
.crates.io
is the official registry for Rust’s open-source libraries, called crates.Cargo.toml
file under the [dependencies]
section.cargo build
, Cargo automatically downloads and compiles all your project’s dependencies.std::env::args()
function.collect()
these into a Vec<String>
(a vector of strings) to easily access them.std::fs
module for file system operations.fs::read_to_string()
function is a simple way to read the entire contents of a file into a string.Result<String, std::io::Error>
..lines()
method gives us an iterator over the lines of the string..contains()
string method to check if it includes our search query.grep
.labs/lab12/README.md
.IS4010: App Development with AI