未能在(盒装)特征对象上应用涡轮鱼符号

Failure to apply turbofish notation on (boxed) trait objects

我可以创建一个指向(堆存储的)File 对象的 Write 特征对象:

let a : Box<dyn Write> = Box::new(File::open("/dev/null").unwrap());  // Compiles fine.

但是,使用 turbofish 表示法会产生错误 (Rust 1.56.1):

let a : Box<dyn Write> = Box::<dyn Write>::new(File::open("/dev/null").unwrap());  // Fails to compile.

错误是:

error[E0599]: the function or associated item `new` exists for struct `Box<dyn std::io::Write>`, but its trait bounds were not satisfied
    --> src/main.rs:37:48
     |
37   |     let a : Box<dyn Write> = Box::<dyn Write>::new(File::open("/dev/null").unwrap());  // Fails to compile.
     |                                                ^^^ function or associated item cannot be called on `Box<dyn std::io::Write>` due to unsatisfied trait bounds
     |
    ::: /home/[REDACTED]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/io/mod.rs:1368:1
     |
1368 | pub trait Write {
     | --------------- doesn't satisfy `dyn std::io::Write: Sized`
     |
     = note: the following trait bounds were not satisfied:
             `dyn std::io::Write: Sized`

一天中的大部分时间我都在研究这个问题。要么我遗漏了一些基本的东西,要么 special language treatment of Box 在这里起作用(但我不知道如何起作用)。

您的代码实际上具有欺骗性。

let a: Box<dyn Write> = Box::new(File::open("/dev/null").unwrap());

实际上并没有像您想象的那样创建 Box<dyn Write>。它首先创建一个 Box<File>,然后将 Box<File> 转换为 Box<dyn Write>。如果您使用 File 进行涡轮捕鱼,那么它会起作用:

let a: Box<dyn Write> = Box::<File>::new(File::open("/dev/null").unwrap());

因此,如果您想明确说明类型,请使用 as 转换而不是 turbofish:

// a is Box<dyn Write>
let a = Box::new(File::open("/dev/null").unwrap()) as Box<dyn Write>;