【发布时间】:2020-04-07 23:01:48
【问题描述】:
我已将泛型应用于结构,并希望使用在结构上实现的 getter 方法以 Option<_, _> 类型获取其输出。
struct Point<T, U> {
x: T,
y: U,
}
impl<T, U> Point<T, U> {
fn x(&self) -> Option<T, U> {
let z = &self.x;
z.get(5);
}
}
fn main() {
let p = Point { x: 5, y: 10.0 };
println!("p.x = {}", p.x());
}
上述代码的输出是
error[E0107]: wrong number of type arguments: expected 1, found 2
--> src/main.rs:6:30
|
6 | fn x(&self) -> Option<T, U> {
| ^ unexpected type argument
【问题讨论】:
-
Option<T>只接受一个类型参数,那么当你使用Option<T, U>时你想要什么? -
您希望打印出来的内容是什么?我不明白您希望
p.x()的结果是什么。