basic/result-error/result #692
Replies: 30 comments 27 replies
-
我是一只新螃蟹,为什么要告诉我try! |
Beta Was this translation helpful? Give feedback.
-
作为一个爬的慢的初学者, 看到这里 已经要靠很用力的回忆才能想起来 |
Beta Was this translation helpful? Give feedback.
-
上面这段话 |
Beta Was this translation helpful? Give feedback.
-
讲真的,从Go过来的我觉得,?真的比if err != nil {}舒服太多了!!! |
Beta Was this translation helpful? Give feedback.
-
expect 那里的报错信息怎么和 unwrap 那里的一样?是复制错了吗? |
Beta Was this translation helpful? Give feedback.
-
因此 ? 只能用于以下形式: |
Beta Was this translation helpful? Give feedback.
-
pub struct XXX<xx: ?Size> ,这里的?有啥用处? |
Beta Was this translation helpful? Give feedback.
-
std::io::error impl error::Error for Error {
#[allow(deprecated, deprecated_in_future)]
fn description(&self) -> &str {
match self.repr.data() {
ErrorData::Os(..) | ErrorData::Simple(..) => self.kind().as_str(),
ErrorData::SimpleMessage(msg) => msg.message,
ErrorData::Custom(c) => c.error.description(),
}
}
#[allow(deprecated)]
fn cause(&self) -> Option<&dyn error::Error> {
match self.repr.data() {
ErrorData::Os(..) => None,
ErrorData::Simple(..) => None,
ErrorData::SimpleMessage(..) => None,
ErrorData::Custom(c) => c.error.cause(),
}
}
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
match self.repr.data() {
ErrorData::Os(..) => None,
ErrorData::Simple(..) => None,
ErrorData::SimpleMessage(..) => None,
ErrorData::Custom(c) => c.error.source(),
}
}
}```
std::io::error 实现了 std::io::error 特征,是不是才是error转换的原因? |
Beta Was this translation helpful? Give feedback.
-
let mut f = match f { |
Beta Was this translation helpful? Give feedback.
-
要是用的第三方库,第三方库里自定义了error,这个就很不舒服了,需要为第三方库的error实现from trait? 这个不就又违法孤儿原则了么, 这个工程上咋解决? |
Beta Was this translation helpful? Give feedback.
-
最后一段: "可以看出 ? 的优势非常明显,何况 ? 还能做链式调用。" |
Beta Was this translation helpful? Give feedback.
-
这两段代码是一样的效果,是因为 fn first(arr: &[i32]) -> Option<&i32> {
arr.get(0)
} fn first(arr: &[i32]) -> Option<&i32> {
let v = arr.get(0)?;
Some(v)
} |
Beta Was this translation helpful? Give feedback.
-
哎,还是Kotlin的 |
Beta Was this translation helpful? Give feedback.
-
这句话其实不准确,报错的原因不是因为没有变量承载,而是返回的类型跟函数的返回值冲突了, error[E0308]: `?` operator has incompatible types 改成这样也能正确运行: #![allow(unused)]
fn main() {
fn first(arr: &[i32]) -> Option<&i32> {
arr.get(0)?
}
} |
Beta Was this translation helpful? Give feedback.
-
match 匹配default 原来不是必须_ ,other_error 看来只要在最后一行,随便命名啊 |
Beta Was this translation helpful? Give feedback.
-
fn first(arr: &[i32]) -> Option<&i32> {
Some(arr.get(1)?)
} 似乎这样写也行 |
Beta Was this translation helpful? Give feedback.
-
作者是个人才,我的笑喷了; |
Beta Was this translation helpful? Give feedback.
-
列举的 fn first(arr: &[i32]) -> Option<&i32> {
Some(arr.get(0)?)
} 而这段代码不能编译通过的原因是 fn first(arr: &[i32]) -> Option<&i32> {
arr.get(0)?
} |
Beta Was this translation helpful? Give feedback.
-
传出去,作者 5 厘米 |
Beta Was this translation helpful? Give feedback.
-
Result<(), Box> 还是有点啰嗦,可以更加简化么? |
Beta Was this translation helpful? Give feedback.
-
在入门实战那儿跳回来补课,发现这可太太太太好用了 |
Beta Was this translation helpful? Give feedback.
-
当看到了 "?" 我的心理打出了一个大大 "?" |
Beta Was this translation helpful? Give feedback.
-
这章看下来终于舒服了,一直在想go的错误处理 |
Beta Was this translation helpful? Give feedback.
-
感觉rust很多想法和ts很像啊 |
Beta Was this translation helpful? Give feedback.
-
Rust 有好多 Typescript 的影子啊 |
Beta Was this translation helpful? Give feedback.
-
一开始对这段话不理解“标准库中的 std::io::Error 和 std::error::Error,前者是 IO 相关的错误结构体,后者是一个最最通用的标准错误特征,同时前者实现了后者,因此 std::io::Error 可以转换为 std:error::Error。”。 问了gpt,大概了是A如果实现了B,那就意味着B中的方法A都包含了,那么A的实例可以转换成B的实例来用。具体的解释如下: 在 Rust 中,如果一个类型(如 这里的类型转换主要是通过特征多态来实现的,这是 Rust 通过特征(traits)实现的一种形式的多态性。这意味着你可以编写函数或者使用 API,这些函数或 API 接受实现了 例如,你可能有一个函数接受 |
Beta Was this translation helpful? Give feedback.
-
fn first(arr: &[i32]) -> Option<&i32> {
arr.get(0)?
} 这段代码的讲解感觉不太合适,这里不是没有值来承接 arr.get(0)? 的值,而是单纯的返回值类型对不上,改成下面这样就能过编译了 fn first(arr: &[i32]) -> Option<&i32> {
Some(arr.get(0)?)
} |
Beta Was this translation helpful? Give feedback.
-
fn open() -> Result<File, Error> {
let path = "example.txt";
File::open(path).or_else(|e| {
if let ErrorKind::NotFound = e.kind() {
File::create(path)
} else {
Err(e)
}
})
} 除了这种模式处理异常外,还有其他模式吗? |
Beta Was this translation helpful? Give feedback.
-
basic/result-error/result
https://course.rs/basic/result-error/result.html
Beta Was this translation helpful? Give feedback.
All reactions