commit
d334367182
|
@ -73,7 +73,7 @@ thread 'main' panicked at 'index out of bounds: the len is 3 but the index is 99
|
|||
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
|
||||
```
|
||||
|
||||
Though there is the panic reason and code line showing here, but sometime we want to get more info about the call stack.
|
||||
Though there is the reason of panic and the line of the code is showing where the panic has occured, sometimes we want to get more info about the call stack.
|
||||
|
||||
3. 🌟
|
||||
```shell
|
||||
|
@ -99,7 +99,7 @@ note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose bac
|
|||
```
|
||||
|
||||
### `unwinding` and `abort`
|
||||
By default, when a `panic` occurs, the program starts *unwinding*, which means Rust walks back up the stack and cleans up the data from each function it encouters.
|
||||
By default, when a `panic` occurs, the program starts *unwinding*, which means Rust walks back up the stack and cleans up the data from each function it encounters.
|
||||
|
||||
But this walk back and clean up is a lot of work. The alternative is to immediately abort the program without cleaning up.
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# result and ?
|
||||
`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`
|
||||
|
||||
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");
|
||||
assert_eq!(result.__, 8);
|
||||
|
||||
println!("Success!")
|
||||
println!("Success!");
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -44,7 +44,7 @@ fn multiply(n1_str: &str, n2_str: &str) -> __ {
|
|||
|
||||
fn main() {
|
||||
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
|
||||
// DON'T change any code else
|
||||
// DON'T change any code lines
|
||||
fn read_file2() -> Result<String, io::Error> {
|
||||
let mut s = String::new();
|
||||
|
||||
|
@ -80,7 +80,7 @@ fn read_file2() -> Result<String, io::Error> {
|
|||
|
||||
fn main() {
|
||||
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() {
|
||||
assert_eq!(add_two("4").unwrap(), 6);
|
||||
|
||||
println!("Success!")
|
||||
println!("Success!");
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -109,7 +109,7 @@ fn main() {
|
|||
use std::num::ParseIntError;
|
||||
|
||||
// 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> {
|
||||
match n1_str.parse::<i32>() {
|
||||
Ok(n1) => {
|
||||
|
@ -146,14 +146,14 @@ fn main() {
|
|||
let tt = multiply("t", "2");
|
||||
print(tt);
|
||||
|
||||
println!("Success!")
|
||||
println!("Success!");
|
||||
}
|
||||
```
|
||||
|
||||
### Type alias
|
||||
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. 🌟
|
||||
```rust,editable
|
||||
|
@ -181,7 +181,7 @@ fn main() {
|
|||
print(multiply("10", "2"));
|
||||
print(multiply("t", "2"));
|
||||
|
||||
println!("Success!")
|
||||
println!("Success!");
|
||||
}
|
||||
```
|
||||
|
||||
|
|
Loading…
Reference in New Issue