result-panic/result #241
Replies: 22 comments 11 replies
-
第一题的题目和答案对不上 |
Beta Was this translation helpful? Give feedback.
-
有大佬解惑吗?这里为什么不能用两个map,前面必须用and_then会报错。 // 提示:使用 |
Beta Was this translation helpful? Give feedback.
-
5题另一种实现: Ok(n1_str.parse::<i32>()? * n2_str.parse::<i32>()?) |
Beta Was this translation helpful? Give feedback.
-
第5题:我是这么写的,这块东西还没讲到 |
Beta Was this translation helpful? Give feedback.
-
main里的return问题 use std::num::ParseIntError;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let number_str = "10";
let number = match number_str.parse::<i32>() {
Ok(number) => number,
Err(e) => return Err(e),
};
println!("{}", number);
Ok(())
} 用 use std::num::ParseIntError;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let number_str = "10";
let number = number_str.parse::<i32>()?;
println!("{}", number);
Ok(())
} |
Beta Was this translation helpful? Give feedback.
-
第一题: // 填空并修复错误
use std::num::ParseIntError;
fn multiply(n1_str: &str, n2_str: &str) -> Result<i32, ParseIntError> {
let n1 = n1_str.parse::<i32>();
let n2 = n2_str.parse::<i32>();
Ok(n1.unwrap() * n2.unwrap())
}
fn main() {
let result = multiply("10", "2");
assert_eq!(result, Ok(20));
let result = multiply("4", "2");
assert_eq!(result.unwrap(), 8);
println!("Success!")
} 第二题: use std::num::ParseIntError;
// 使用 `?` 来实现 multiply
// 不要使用 unwrap !
fn multiply(n1_str: &str, n2_str: &str) -> Result<i32, ParseIntError> {
Ok(n1_str.parse::<i32>()? * n2_str.parse::<i32>()?)
}
fn main() {
assert_eq!(multiply("3", "4").unwrap(), 12);
println!("Success!")
} 第三题: use std::fs::File;
use std::io::{self, Read};
fn read_file1() -> Result<String, io::Error> {
let f = File::open("hello.txt");
let mut f = match f {
Ok(file) => file,
Err(e) => return Err(e),
};
let mut s = String::new();
match f.read_to_string(&mut s) {
Ok(_) => Ok(s),
Err(e) => Err(e),
}
}
// 填空
// 不要修改其它代码
fn read_file2() -> Result<String, io::Error> {
let mut s = String::new();
File::open("hello.txt")?.read_to_string(&mut s)?;
Ok(s)
}
fn main() {
assert_eq!(read_file1().unwrap_err().to_string(), read_file2().unwrap_err().to_string());
println!("Success!")
} 第四题: use std::num::ParseIntError;
// 使用两种方式填空: map, and then
fn add_two1(n_str: &str) -> Result<i32, ParseIntError> {
n_str.parse::<i32>().map(|n| n + 2)
}
fn add_two2(n_str: &str) -> Result<i32, ParseIntError> {
n_str.parse::<i32>().and_then(|n| Ok(n + 2))
}
fn main() {
assert_eq!(add_two1("4").unwrap(), 6);
assert_eq!(add_two2("4").unwrap(), 6);
println!("Success!")
} 第五题: use std::num::ParseIntError;
// 使用 Result 重写后,我们使用模式匹配的方式来处理,而无需使用 `unwrap`
// 但是这种写法实在过于啰嗦..
fn multiply(n1_str: &str, n2_str: &str) -> Result<i32, ParseIntError> {
match n1_str.parse::<i32>() {
Ok(n1) => {
match n2_str.parse::<i32>() {
Ok(n2) => {
Ok(n1 * n2)
},
Err(e) => Err(e),
}
},
Err(e) => Err(e),
}
}
// 重写上面的 `multiply` ,让它尽量简介
// 提示:使用 `and_then` 和 `map`
fn multiply1(n1_str: &str, n2_str: &str) -> Result<i32, ParseIntError> {
// 实现...
Ok(n1_str.parse::<i32>()? * n2_str.parse::<i32>()?)
}
fn print(result: Result<i32, ParseIntError>) {
match result {
Ok(n) => println!("n is {}", n),
Err(e) => println!("Error: {}", e),
}
}
fn main() {
let twenty = multiply1("10", "2");
print(twenty);
// 下面的调用会提供更有帮助的错误信息
let tt = multiply("t", "2");
print(tt);
println!("Success!")
} 第六题: use std::num::ParseIntError;
// 填空
type Res<T> = Result<T, ParseIntError>;
// 使用上面的别名来引用原来的 `Result` 类型
fn multiply(first_number_str: &str, second_number_str: &str) -> Res<i32> {
first_number_str.parse::<i32>().and_then(|first_number| {
second_number_str.parse::<i32>().map(|second_number| first_number * second_number)
})
}
// 同样, 这里也使用了类型别名来简化代码
fn print(result: Res<i32>) {
match result {
Ok(n) => println!("n is {}", n),
Err(e) => println!("Error: {}", e),
}
}
fn main() {
print(multiply("10", "2"));
print(multiply("t", "2"));
println!("Success!")
} |
Beta Was this translation helpful? Give feedback.
-
前3题,关注 // 填空并修复错误
use std::num::ParseIntError;
fn multiply(n1_str: &str, n2_str: &str) -> Result<i32, ParseIntError> {
let n1 = n1_str.parse::<i32>();
let n2 = n2_str.parse::<i32>();
Ok(n1.unwrap() * n2.unwrap())
}
fn main() {
let result = multiply("10", "2");
assert_eq!(result, Ok(20));
let result = multiply("4", "2");
assert_eq!(result.unwrap(), 8);
println!("Success!")
} 2、 use std::num::ParseIntError;
// 使用 `?` 来实现 multiply
// 不要使用 unwrap !
fn multiply(n1_str: &str, n2_str: &str) -> Result<i32, ParseIntError>{
let n1 = n1_str.parse::<i32>();
let n2 = n2_str.parse::<i32>();
Ok(n1?*n2?)
}
fn main() {
assert_eq!(multiply("3", "4").unwrap(), 12);
println!("Success!")
} 3、 use std::fs::File;
use std::io::{self, Read};
fn read_file1() -> Result<String, io::Error> {
let f = File::open("hello.txt");
let mut f = match f {
Ok(file) => file,
Err(e) => return Err(e),
};
let mut s = String::new();
match f.read_to_string(&mut s) {
Ok(_) => Ok(s),
Err(e) => Err(e),
}
}
// 填空
// 不要修改其它代码
fn read_file2() -> Result<String, io::Error> {
let mut s = String::new();
File::open("hello.txt")?.read_to_string(&mut s)?;
Ok(s)
}
fn main() {
assert_eq!(read_file1().unwrap_err().to_string(), read_file2().unwrap_err().to_string());
println!("Success!")
} |
Beta Was this translation helpful? Give feedback.
-
use std::num::ParseIntError;
// 使用 `?` 来实现 multiply
// 不要使用 unwrap !
fn multiply(n1_str: &str, n2_str: &str) -> Result<i32, ParseIntError> {
Ok(n1_str.parse::<i32>()? * n2_str.parse::<i32>()?)
}
fn main() {
assert_eq!(multiply("3", "4").unwrap(), 12);
println!("Success!")
} |
Beta Was this translation helpful? Give feedback.
-
如果我的函数中可能会出现两个以上不同类型的Error,怎么使用Result啊 |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
map 和 and then什么时候讲的 闭包参数不是到后面才有吗 |
Beta Was this translation helpful? Give feedback.
-
第五题这样写也能通过 |
Beta Was this translation helpful? Give feedback.
-
如果有人知道 Haskell 的话,Rust 的 map 就是 Haskell 的 fmap,而 and_then 就是 Haskell 的 >>=。 fmap :: Functor f => f a -> (a -> b) -> f b 对应到 Rust 就是:
(>>=)[and_then]:
|
Beta Was this translation helpful? Give feedback.
-
第五题可以用一种更加 Haskell 式(或者 Async/Await 式)的方法来写 use std::num::ParseIntError;
use donotation::m;
// 使用 Result 重写后,我们使用模式匹配的方式来处理,而无需使用 `unwrap`
// 但是这种写法实在过于啰嗦..
fn multiply(n1_str: &str, n2_str: &str) -> Result<i32, ParseIntError> {
match n1_str.parse::<i32>() {
Ok(n1) => {
match n2_str.parse::<i32>() {
Ok(n2) => {
Ok(n1 * n2)
},
Err(e) => Err(e),
}
},
Err(e) => Err(e),
}
}
// 重写上面的 `multiply` ,让它尽量简洁
// 提示:使用 `and_then` 和 `map`
fn multiply1(n1_str: &str, n2_str: &str) -> Result<i32, ParseIntError> {
m! {
x <- n1_str.parse::<i32>();
y <- n2_str.parse::<i32>();
return x * y;
}
}
fn print(result: Result<i32, ParseIntError>) {
match result {
Ok(n) => println!("n is {}", n),
Err(e) => println!("Error: {}", e),
}
}
fn main() {
let twenty = multiply1("10", "2");
print(twenty);
// 下面的调用会提供更有帮助的错误信息
let tt = multiply("t", "2");
print(tt);
println!("Success!")
} |
Beta Was this translation helpful? Give feedback.
-
第五题的四种写法: // fn multiply1(n1_str: &str, n2_str: &str) -> Result<i32, ParseIntError> {
// // 实现...
// n1_str.parse::<i32>().map(|n1| n1 * n2_str.parse::<i32>().unwrap())
// }
// fn multiply1(n1_str: &str, n2_str: &str) -> Result<i32, ParseIntError> {
// n1_str.parse::<i32>().map(|n1| {
// n2_str.parse::<i32>().map(|n2| n2 * n1)
// })? // 不加?的类型: Result<Result<i32, ParseIntError>, ParseIntError>
// }
// fn multiply1(n1_str: &str, n2_str: &str) -> Result<i32, ParseIntError> {
// n1_str.parse::<i32>().map(|n1| {
// n2_str.parse::<i32>().and_then(|n2| Ok(n2 * n1))
// })? // 不加?的类型: Result<Result<i32, ParseIntError>, ParseIntError>
// }
fn multiply1(n1_str: &str, n2_str: &str) -> Result<i32, ParseIntError> {
n1_str.parse::<i32>().and_then(|n1| {
n2_str.parse::<i32>().map(|n2| n2 * n1)
})
} |
Beta Was this translation helpful? Give feedback.
-
mark finished |
Beta Was this translation helpful? Give feedback.
-
第3题不对吧,如果有 hello.txt 文件,在main函数中unwrap_err 不就 panic 了吗?“Success!” 不能 |
Beta Was this translation helpful? Give feedback.
-
一个问题,这里的?宏运算,我直接这样返回也是可以的,为啥还需要?运算,有咩有一个好的例子说明传播错误的重要性,以及?在这里面的作用。
|
Beta Was this translation helpful? Give feedback.
-
result-panic/result
Learning Rust By Practice, narrowing the gap between beginner and skilled-dev with challenging examples, exercises and projects.
https://zh.practice.rs/result-panic/result.html
Beta Was this translation helpful? Give feedback.
All reactions