不能重复泛型的引用

问题描述 投票:0回答:0

我有一个包含两个泛型的结构

    pub struct Rectangle<T, U> {
    height: T,
    width: U,
    }

带有 impl 块

    impl< T, U> Rectangle<T, U>
{
    pub fn new(height: T, width: U) -> Self {
        Self { height, width }
    }

    pub fn area(&self) -> &U {
        &self.width * &self.height // Error: cannot multiply `&U` by `&T`
    }
}

即使在 Mul impl 之后它也不起作用

如果 ref(&) 被移除,工作正常。

我认为有效的方法是传递引用而不是复制。我怎样才能使这段代码通过引用进行编译。 带有参考的代码

generics rust struct lifetime
© www.soinside.com 2019 - 2024. All rights reserved.