mirror of
https://github.com/sunface/rust-by-practice.git
synced 2025-06-23 04:29:41 +00:00
Fixed some grammar mistakes
This commit is contained in:
@ -1,7 +1,7 @@
|
|||||||
# result and ?
|
# result and ?
|
||||||
`Result<T>` is an enum to describe possible errors. It has two variants:
|
`Result<T>` is an enum to describe possible errors. It has two variants:
|
||||||
|
|
||||||
- `Ok(T)`: a value T was found
|
- `Ok(T)`: A value T was found
|
||||||
- `Err(e)`: An error was found with a value `e`
|
- `Err(e)`: An error was found with a value `e`
|
||||||
|
|
||||||
In short words, the expected outcome is `Ok`, while the unexpected outcome is `Err`.
|
In short words, the expected outcome is `Ok`, while the unexpected outcome is `Err`.
|
||||||
@ -25,7 +25,7 @@ fn main() {
|
|||||||
let result = multiply("t", "2");
|
let result = multiply("t", "2");
|
||||||
assert_eq!(result.__, 8);
|
assert_eq!(result.__, 8);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -44,7 +44,7 @@ fn multiply(n1_str: &str, n2_str: &str) -> __ {
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
assert_eq!(multiply("3", "4").unwrap(), 12);
|
assert_eq!(multiply("3", "4").unwrap(), 12);
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -69,7 +69,7 @@ fn read_file1() -> Result<String, io::Error> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FILL in the blanks with one code line
|
// FILL in the blanks with one code line
|
||||||
// DON'T change any code else
|
// DON'T change any code lines
|
||||||
fn read_file2() -> Result<String, io::Error> {
|
fn read_file2() -> Result<String, io::Error> {
|
||||||
let mut s = String::new();
|
let mut s = String::new();
|
||||||
|
|
||||||
@ -80,7 +80,7 @@ fn read_file2() -> Result<String, io::Error> {
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
assert_eq!(read_file1().unwrap_err().to_string(), read_file2().unwrap_err().to_string());
|
assert_eq!(read_file1().unwrap_err().to_string(), read_file2().unwrap_err().to_string());
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -100,7 +100,7 @@ fn add_two(n_str: &str) -> Result<i32, ParseIntError> {
|
|||||||
fn main() {
|
fn main() {
|
||||||
assert_eq!(add_two("4").unwrap(), 6);
|
assert_eq!(add_two("4").unwrap(), 6);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -109,7 +109,7 @@ fn main() {
|
|||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
// With the return type rewritten, we use pattern matching without `unwrap()`.
|
// With the return type rewritten, we use pattern matching without `unwrap()`.
|
||||||
// But it's so Verbose..
|
// But it's so Verbose...
|
||||||
fn multiply(n1_str: &str, n2_str: &str) -> Result<i32, ParseIntError> {
|
fn multiply(n1_str: &str, n2_str: &str) -> Result<i32, ParseIntError> {
|
||||||
match n1_str.parse::<i32>() {
|
match n1_str.parse::<i32>() {
|
||||||
Ok(n1) => {
|
Ok(n1) => {
|
||||||
@ -146,14 +146,14 @@ fn main() {
|
|||||||
let tt = multiply("t", "2");
|
let tt = multiply("t", "2");
|
||||||
print(tt);
|
print(tt);
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Type alias
|
### Type alias
|
||||||
Using `std::result::Result<T, ParseIntError>` everywhere is verbose and tedious, we can use alias for this purpose.
|
Using `std::result::Result<T, ParseIntError>` everywhere is verbose and tedious, we can use alias for this purpose.
|
||||||
|
|
||||||
At a module level, creating aliases can be particularly helpful. Errors found in the a specific module often has the same `Err` type, so a single alias can succinctly defined all associated `Results`. This is so useful even the `std` library even supplies one: [`io::Result`](https://doc.rust-lang.org/std/io/type.Result.html).
|
At a module level, creating aliases can be particularly helpful. Errors found in a specific module often has the same `Err` type, so a single alias can succinctly defined all associated `Results`. This is so useful even the `std` library even supplies one: [`io::Result`](https://doc.rust-lang.org/std/io/type.Result.html).
|
||||||
|
|
||||||
6. 🌟
|
6. 🌟
|
||||||
```rust,editable
|
```rust,editable
|
||||||
@ -181,7 +181,7 @@ fn main() {
|
|||||||
print(multiply("10", "2"));
|
print(multiply("10", "2"));
|
||||||
print(multiply("t", "2"));
|
print(multiply("t", "2"));
|
||||||
|
|
||||||
println!("Success!")
|
println!("Success!");
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user