【发布时间】:2021-01-30 13:54:28
【问题描述】:
我只是想传播 IO 错误:
enum MyError {
EOF,
IO(std::io::Error),
}
fn peek_byte<R>(mut p: Peekable<Bytes<R>>) -> Result<u8, MyError>
where
R: Read,
{
match p.peek() {
None => Err(MyError::EOF),
Some(Err(e)) => Err(MyError::IO(*e)), // <==== error is here
Some(Ok(byte)) => Ok(*byte),
}
}
但是,我收到以下错误:
error[E0507]: cannot move out of `*e` which is behind a shared reference
--> src/main.rs:17:41
|
17 | Some(Err(e)) => Err(MyError::IO(*e)),
| ^^ move occurs because `*e` has type `std::io::Error`, which does not implement the `Copy` trait
我其实明白这一切。我知道为什么会出现错误,以及错误的含义。我不知道的是如何完成我的任务并将 IO 错误传播到我的错误类型中。
我尝试过e、*e、e.clone()、*e.clone()、*(e.clone()),但它们都产生“类型不匹配”或“无法移动”错误。
【问题讨论】:
标签: error-handling rust iterator borrow-checker ownership