我有这个功能:
pub async fn player(&self, id: &str) -> Result<Option<Box<dyn AsyncRead + Send>>> {
// use id here...
let file = tokio::fs::File::open("player.pdf").await?;
let res = Some(Box::new(file));
Ok(res)
}
它不起作用:
error[E0308]: mismatched types
|
46 | Ok(res)
| -- ^^^ expected trait object `dyn tokio::io::AsyncRead`, found struct `tokio::fs::File`
| |
| arguments to this enum variant are incorrect
|
= note: expected enum `std::option::Option<std::boxed::Box<dyn tokio::io::AsyncRead + std::marker::Send>>`
found enum `std::option::Option<std::boxed::Box<tokio::fs::File>>`
note: tuple variant defined here
--> C:\Users\Fred\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib/rustlib/src/rust\library\core\src\result.rs:508:5
|
508 | Ok(#[stable(feature = "rust1", since = "1.0.0")] T),
| ^^
但如果我go 掉Option<>
的部分就行了.为什么?