From the course: Rust Essential Training
Unlock the full course today
Join today to access over 22,600 courses taught by industry experts or purchase this course individually.
Propagating errors - Rust Tutorial
From the course: Rust Essential Training
Propagating errors
- [Teacher] Sometimes when code within a function has the potential to produce an error, rather than handling that error within the function you may want to propagate it upwards to the higher level code that called the function where more information and context may be available to better decide how to deal with that error. Consider the read and combine function shown here on line four. It takes the path of the two input files, reads both of their contents, and then combines their contents to produce a single output string. Notice that the return type for this function is a result enum, which will either be the okay variant containing the combined string or an error. Since the read to string function that it uses has the potential to fail with an IO error and returns a result enum, the match statements on lines five and nine check those results and if either one produces an error it will return that error, propagating it…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.