鉴于这个最小可复现示例:
fn main() {
println!("{}", foo(None));
}
trait Trait {}
struct Struct {}
impl Trait for Struct {}
fn foo(maybe_trait: Option<&impl Trait>) -> String {
return "hello".to_string();
}
Rust编译器不高兴:
error[E0282]: type annotations needed
--> src\main.rs:2:20
|
2 | println!("{}", foo(None));
| ^^^ cannot infer type for `impl Trait`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0282`.
使用类型注释可以使这个代码编译通过:
fn main() {
let nothing: Option<&Struct> = None;
println!("{}", foo(nothing));
}
trait Trait {}
struct Struct {}
impl Trait for Struct {}
fn foo(maybe_trait: Option<&impl Trait>) -> String {
return "hello".to_string();
}
如果我们在类型注释中使用
Trait
而不是Struct
,会向我们提供更多的信息:warning: trait objects without an explicit `dyn` are deprecated
--> src\main.rs:2:26
|
2 | let nothing: Option<&Trait> = None;
| ^^^^^ help: use `dyn`: `dyn Trait`
|
= note: #[warn(bare_trait_objects)] on by default
error[E0277]: the size for values of type `dyn Trait` cannot be known at compilation time
--> src\main.rs:3:20
|
3 | println!("{}", foo(nothing));
| ^^^ doesn't have a size known at compile-time
|
= help: the trait `std::marker::Sized` is not implemented for `dyn Trait`
= note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
note: required by `foo`
--> src\main.rs:10:1
|
10| fn foo(maybe_trait: Option<&impl Trait>) -> String {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.
我理解为“您不应在此处使用Trait,因为我不知道需要为此参数分配多少内存”。
但是,当我传递None
时,这与相关吗?
当然,传递实现了Trait
(即Struct
)的类型的任何具体实例对编译器都是可以的。
附注:
我已经阅读了此答案,介绍了&dyn Trait
和&impl Trait
之间的区别。我不确定何时使用哪种方法,但由于我的程序使用类型注释(如上所示)和&impl Trait
进行编译,因此它似乎是安全的选择。
如果我们将函数参数的类型设置为Option<&dyn Trait>
,则我的程序在main()
中没有类型注释的情况下编译:
fn main() {
println!("{}", foo(None));
}
trait Trait {}
struct Struct {}
impl Trait for Struct {}
fn foo(maybe_trait: Option<&dyn Trait>) -> String {
return "hello".to_string();
}
$ cargo --version
cargo 1.37.0 (9edd08916 2019-08-02)
$ cat Cargo.toml
[package]
name = "rdbug"
version = "0.1.0"
authors = ["redacted"]
edition = "2018"
foo::HugeThing
即使传入None
,其行为可能与foo::OtherThing
不同,编译器必须知道使用正确的函数。 - trent