Result is an enumeration, accessed via the prelude

You may have noticed already, but the Result type with its Ok and Err values looks an awful lot like an enumeration. That's because it is an enumeration, which is how it's able to contain different data types depending on whether it represents an error or a success. It's an enumeration with generic type parameters, so we won't be able to pull off quite the same functionality until we learn about those but, at its base, it's still just an enumeration.

We can use Ok and Err instead of Result::Ok and Result::Err when we write functions that might fail or handle the results of such functions, because those values are added directly to the Rust prelude for our convenience. Actually, we can use the Result type without saying where it came from for the same reason.

The prelude is a collection of very basic and useful data types, values, and traits that is automatically made available in every Rust module. It contains such things as String, Vec (a vector, which has the same relation to arrays that String does to str), and Box (which we'll talk about in the next chapter).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.237.79