inflearn logo

Elegant Rust Intermediate (5) - Option & Result

When you encounter an Option or Result, do you reach for unwrap() first? Learn to distinguish between missing values and failed operations, and how to chain processing steps with map, and_then, and ?. Through runnable examples and comprehensive exercises, you’ll learn to decide for yourself how far to propagate errors and where to handle them.

12 learners are taking this course

Level Intermediate

Course period Unlimited

Rust
Rust
Rust
Rust

What you will gain after the course

  • You can distinguish between cases where there is no value and cases where an operation fails by representing them with Option and Result.

  • Instead of `unwrap()`, you can choose an appropriate way to handle default values and errors depending on the situation.

  • You can concisely chain multiple processing steps using `map`, `and_then`, and `?`.

  • You can use `collect` and `transpose` to gather multiple results or transform nested `Option`s and `Result`s.

  • You can define the types of errors as an enum and distinguish between errors to pass on to the caller and errors to handle directly.

Writing Rust Code with Clear Next Steps Even When Values Are Missing or Operations Fail

When writing Rust code, you will often encounter Option and Result. At first, you may use unwrap() to extract a value, but when there is no input or an unexpected value is provided, you may start wondering how to handle it.

In this course, we start by distinguishing between cases where a value is absent and cases where an operation fails. You will learn, step by step, how to set default values, chain multiple processing steps, and pass errors back to the caller. Rather than memorizing API names, we examine what the caller of a function needs to know and what decisions they need to make.

You can apply this when validating user input, converting strings to numbers, reading files, or collecting the results of multiple operations. By running the examples yourself and completing a comprehensive exercise, you will practice designing error-handling code on your own.

What you will learn

(1) Handling Cases Where a Value Is Absent with Option

Understand the meaning of Some and None and decide whether to use a default value or pass it through as is when no value is available. Examine unwrap_or and unwrap_or_else to compare them, then map and and_then to chain value transformations. You will also learn how to borrow the inner value without transferring ownership or take to take a value out.

The difference between map and and_then

(2) Expressing the Reason for Failure with Result

Ok and Err distinguish success from failure, and you include the reason for failure in your code. map_err changes the error type and uses enums to represent different errors. Write functions so that the caller can respond to failures instead of unconditionally extracting values.

(3) Propagating Errors with ? and Handling Them Where Necessary

We examine cases where ? extracts a value and returns early from a function. Depending on the function's return type, understand ?how to use it, and distinguish between propagating errors in a library and notifying users in a program.

Writing Clean Functions with ?

(4) Collecting Multiple Results and Simplifying Nested Types

Use collect to gather multiple Result values into a single result, and see what happens when an error occurs during processing. We’ll also use ok_or, ok, and transpose to convert between Option and Result and examine which information is preserved or lost.

(5) Check your understanding with comprehensive exercises and quizzes

Implement functions that transform input values and distinguish calculation errors. Test not only valid inputs but also cases where values are missing or invalid inputs are provided. In the final quiz, review what you have learned by checking the code’s execution results and the reasons for choosing particular APIs.

Notes Before Taking the Course

Practice Environment

  • You need a Windows, macOS, or Linux environment capable of running Rust and Cargo.

  • Please prepare a stable version of Rust that supports the Rust 2024 edition.

  • You can practice using your usual code editor and terminal.

Learning materials

  • A web-based textbook containing chapter-by-chapter explanations and code

  • Example source code that you can run directly with Cargo

  • Comprehensive practice code and automated tests

  • Multiple-choice quizzes and explanations to review the key concepts

Prerequisite Knowledge and Notes

We assume you understand the basic concepts of Rust variables, functions, conditionals, structs, and enums, as well as ownership and borrowing. If you can read code that distinguishes values using match, you will be able to follow along more easily.

Before running the examples, try to predict the results first. After running them, also change the inputs to check cases where there is no value and cases where an error occurs. In the comprehensive exercise, in addition to passing the tests, we recommend explaining why you chose that return type and error-handling method.

Recommended for
these people

Who is this course right for?

  • Those who have learned Rust’s basic syntax but get stuck whenever they work with Option and Result

  • For those who want to improve code that relies on `unwrap()` or repetitive matches

  • Those who want to understand exactly when ? can be used and how errors are propagated.

  • Those who want to implement input validation and error handling themselves and verify them through testing

Need to know before starting?

  • You should be familiar with Rust’s basic syntax, including variables, functions, and conditional statements.

  • It would be good to understand the basic concepts of ownership, borrowing, and references.

  • You should be able to read code that defines structs and enums and distinguishes values using match.

  • Knowing how to run and test examples with Cargo will make it easier to follow along with the exercises.

Hello
This is hatemogi

Career Verified

2,264

Learners

176

Reviews

27

Answers

4.7

Rating

11

Courses

I am a backend developer with a strong interest in functional programming. I have led development teams as a development team manager at Kakao, a principal engineer at NHN, and a lead at LINE+. I also worked as the head of the development team at an AI startup called Dnotitia, where I worked on projects using Rust.

Curriculum

All

18 lectures ∙ (1hr 29min)

Published: 
Last updated: 

Reviews

Not enough reviews.
Please write a valuable review that helps everyone!

hatemogi's other courses

Check out other courses by the instructor!

Similar courses

Explore other courses in the same field!

Limited time deal

$146,692.00

37%

$8.80