鉴于以下内容:
's'
我可以用它作为:
use std::fmt::Debug;
#[derive(Debug)]
enum A<T: Debug> {
X,
Y(T),
}
#[derive(Debug)]
struct B;
type C = A<B>;
// use A<B> as C; // Does not compile
但是对于多个通用参数(或fn main() {
let val0 = A::X::<B>;
let val1 = A::Y::<B>(B);
println!("{:?}\t{:?}", val0, val1);
}
,A
等更长的名称然后别名它我尝试了以下但它不编译:
B
有错误:
fn main() {
let val0 = C::X;
let val1 = C::Y(B);
println!("{:?}\t{:?}", val0, val1);
}
同样指出我无法使用src/main.rs:656:16: 656:20 error: no associated item named `X` found for type `A<B>` in the current scope
src/main.rs:656 let val0 = C::X;
^~~~
src/main.rs:657:16: 657:20 error: no associated item named `Y` found for type `A<B>` in the current scope
src/main.rs:657 let val1 = C::Y(B);
来解决问题。有没有办法(因为输入整个东西似乎很麻烦)?
use
答案 0 :(得分:4)
有没有解决方法(因为输入整个东西似乎很麻烦)?
您可以指定C
作为变量的类型,这样您就可以使用A::X
或A::Y
而无需明确指定类型参数:
let val0: C = A::X;
let val1: C = A::Y(B);