1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-01-09 09:11:50 +02:00

Add a map_err in speaker notes (#2155)

The type returned by `String::from_utf8(raw)`, is `Result<_>` and needs
to be mapped to match the type of the return type of `next`. You get
this error otherwise:

```
    Compiling playground v0.0.1 (/playground)
warning: unused import: `ErrorKind`
 --> src/main.rs:1:21
  |
1 | use std::io::{self, ErrorKind};
  |                     ^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

error[E0308]: mismatched types
   --> src/main.rs:29:17
    |
29  |         Ok(Some(s))
    |            ---- ^ expected `String`, found `Result<String, FromUtf8Error>`
    |            |
    |            arguments to this enum variant are incorrect
    |
    = note: expected struct `String`
                 found enum `Result<String, FromUtf8Error>`
help: the type constructed contains `Result<String, FromUtf8Error>` due to the type of the argument passed
   --> src/main.rs:29:12
    |
29  |         Ok(Some(s))
    |            ^^^^^-^
    |                 |
    |                 this argument influences the type of `Some`
note: tuple variant defined here
   --> /playground/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/option.rs:579:5
    |
579 |     Some(#[stable(feature = "rust1", since = "1.0.0")] T),
    |     ^^^^
help: use the `?` operator to extract the `Result<String, FromUtf8Error>` value, propagating a `Result::Err` value to the caller
    |
29  |         Ok(Some(s?))
    |                  +

For more information about this error, try `rustc --explain E0308`.
warning: `playground` (bin "playground") generated 1 warning
error: could not compile `playground` (bin "playground") due to 1 previous error; 1 warning emitted
```
This commit is contained in:
Luke Yeh 2024-06-24 09:57:00 -04:00 committed by GitHub
parent 9692d822db
commit 1fb284640e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -102,6 +102,7 @@ async fn main() -> std::io::Result<()> {
// ...
let raw = std::mem::take(&mut self.bytes);
let s = String::from_utf8(raw)
.map_err(|_| io::Error::new(ErrorKind::InvalidData, "not UTF-8"))?;
// ...
}
}