我看到一些看起来像这样的Rust代码:
#![feature(default_type_params)]
Run Code Online (Sandbox Code Playgroud)
经过大量的谷歌搜索,我什么都没得到.这是什么?我应该什么时候使用它?
它是启用功能,称为默认类型参数,但您已经知道,所以让我给您一个示例:
#![feature(default_type_params)]
struct Foo<A=(int, char)> { // default type parameter here!
a: A
}
fn default_foo(x: Foo) {
let (_i, _c): (int, char) = x.a;
}
fn main() {
default_foo(Foo { a: (1, 'a') })
}
Run Code Online (Sandbox Code Playgroud)
如果没有默认类型参数,则需要显式设置参数:
struct Foo<A> {
a: A
}
fn default_foo(x: Foo<(int, char>)) {
let (_i, _c): (int, char) = x.a;
}
fn main() {
default_foo(Foo { a: (1, 'a') })
}
Run Code Online (Sandbox Code Playgroud)
从这里偷来的例子:https://github.com/rust-lang/rust/pull/11217