Loading tutorials
Option<T> is defined, how you create and inspect one, and the mistakes people commonly make with it. It assumes you know basic Rust syntax, enums and match, ownership and borrowing, and generics at a conceptual level. The previous chapter explained why Rust has no null. T4 min readmatch, basic generics, and the idea that a function can return a value of a type you choose. It builds on the previous chapter, where Option<T> represented a value that might be missing. Result<T, E> covers a different case: the operation ran, and i4 min readOption<T> and Result<T, E> as ordinary enums. Here you learn the three constructs that take those values apart: match, if let, and let else. They differ 5 min readOption and Result, match, and the basics of ownership. By the end you should be able to explain what unwrap and expect do, what a panic costs, and when each shortcut is acceptable.5 min readmatch, closures, and the shape of Option<T> (Some(value) or None). The previous chapters showed how to take an Option apart with match, if let, and let else. Here you will transform one without unpacking it, which keeps the c4 min readmatch on Result, you know closures at a basic level, and you have seen Option combinators such as map and and_then. Here you will use the same ideas on Result<T, E>, where there are two sides to transform: the success value and the error.3 min readOption<T> and Result<T, E> are defined, how to handle them with match, if let, and let else, and how closures work. It covers the methods that convert one type into the other, and when each conversion is the right choice.4 min readmatch on Option and Result, and that you know the basic combinators from earlier chapters. You should also be comfortable with function return types and with the idea that a trait such as From converts one type into another.4 min readenum and match on it, you know what a trait is and how to implement one, and you have used ? and map_err on a Result. Error conversion with From gets its own chapter. Here you will only see it in passing.4 min read? operator converts one error type into another, and how to set that up with the From trait. It assumes you know how ? propagates errors, how to write an enum with Display and Error impls, and what a trait impl is. The rest is built from those pieces.4 min read? works, how From conversions are used for errors, and what a trait is at a conceptual level. The previous chapters showed a custom error enum with a From implementation for each error source. That approach is precise but takes a lot of code. When a function c4 min read? and the Result type. It covers what main is allowed to return, what the runtime prints when main returns an Err, and how to control the exit code.4 min readOption, Result, map, and the ? operator from earlier chapters. It covers a common situation. You run a fallible step over many items and want one outcome for the whole batch, not a pile of individual Result 4 min readmatch and if let work on Option, and how closures are passed to methods like map. It covers what happens when an Option holds a value that is not Copy, such as a String or a Box. It also covers the three methods that ha4 min readDisplay, Error, and From by hand. It also assumes you know how ? converts errors and how Box<dyn Error> works. Two crates, thiserror and anyhow, remove most of the repetitive code from that work. Here you will learn w5 min readOption, Result, the ? operator, From conversions, and how unwrap and expect behave. It turns those tools into design rules for your own functions. The rules decide what a function returns, what it panics on, and which error type it exposes.5 min read