impl Trait
与特征边界类似,impl Trait 语法可以在函数形参 和返回值中使用:
1. // Syntactic sugar for:
2. // fn add_42_millions<T: Into<i32>>(x: T) -> i32 {
3. fn add_42_millions(x: impl Into<i32>) -> i32 {
4. x.into() + 42_000_000
5. }
7. fn pair_of(x: u32) -> impl std::fmt::Debug {
8. (x + 1, x - 1)
9. }
11. fn main() {
12. let many = add_42_millions(42_i8);
13. println!("{many}");
14. let many_more = add_42_millions(10_000_000);
15. println!("{many_more}");
16. let debuggable = pair_of(27);
17. println!("debuggable: {debuggable:?}");
18. }
This slide should take about 5 minutes.
impl Trait allows you to work with types which you cannot name. The meaning of impl Trait is a bit different in the different positions.
- 对形参来说,
impl Trait就像是具有特征边界的匿名泛型形参。 - 对返回值类型来说,它则意味着返回值类型就是实现该特征的某具体类型, 无需为该类型命名。如果您不想在公共 API 中公开该具体类型,便可 使用此方法。
在返回位置处进行推断有一定难度。会返回 impl Foo 的函数会挑选 自身返回的具体类型,而不必在来源中写出此信息。会返回 泛型类型(例如 collect<B>() -> B)的函数则可返回符合 B 的任何类型,而调用方可能需要选择一个类型,例如使用 let x: Vec<_> = foo.collect() 或使用以下 Turbofish:foo.collect::<Vec<_>>()。
debuggable 是什么类型?尝试输入 let debuggable: () = ..,查看会显示什么错误消息。
