mirror of
https://github.com/rust-lang-cn/book-cn.git
synced 2025-01-26 09:18:42 +08:00
Remove 'creating your own error types'
this should end up later in the book, someday
This commit is contained in:
parent
e7b5943c35
commit
e35e0e1039
@ -6,7 +6,6 @@
|
||||
|
||||
- [Guessing Game Tutorial](ch02-00-guessing-game-tutorial.md)
|
||||
|
||||
<<<<<<< a2bc57618340b6257b9a4dd7d4f3993c71cde2e4
|
||||
- [Common Programming Concepts in Rust](ch03-00-common-programming-concepts-in-rust.md)
|
||||
- [Variable Bindings and Mutability](ch03-01-variable-bindings-and-mutability.md)
|
||||
- [Data Types](ch03-02-data-types.md)
|
||||
|
@ -175,3 +175,6 @@ effects; nothing is returned upon success. Well, functions with no return type,
|
||||
as we just saw with `main()`, are the same as returning unit. So we can use
|
||||
it as the return type here, too. This leads to the last line of the function,
|
||||
the slightly silly-looking `Ok(())`. This is an `Ok()` with a `()` inside.
|
||||
|
||||
In chapter XX, we'll learn how to make our own types like these, but for now,
|
||||
an understanding of the core `Result<T, E>` is enough.
|
||||
|
@ -1,34 +0,0 @@
|
||||
# Creating your own Error types
|
||||
|
||||
This pattern of "return an error" is so common, many libraries create their
|
||||
own error type, and use it for all of their functions. We can re-write the
|
||||
previous example to use `std::io::Result` rathern than a regular `Result`:
|
||||
|
||||
```rust
|
||||
#![feature(question_mark)]
|
||||
|
||||
use std::fs::File;
|
||||
use std::io;
|
||||
|
||||
fn main() {
|
||||
}
|
||||
|
||||
pub fn process_file() -> io::Result<()> {
|
||||
let f = File::open("hello.txt")?;
|
||||
|
||||
// do some stuff with f
|
||||
|
||||
Ok(())
|
||||
}
|
||||
```
|
||||
|
||||
`io::Result` looks like this:
|
||||
|
||||
```rust
|
||||
# use std::io;
|
||||
type Result<T> = Result<T, std::io::Error>;
|
||||
```
|
||||
|
||||
It's a type alias for a regular-old `Result<T, E>`, with the `E` set up to be a
|
||||
`std::io::Error`. This means we don't need to worry about the error type in our
|
||||
function signatures, which is nice.
|
Loading…
Reference in New Issue
Block a user