Why does cloning my custom type result in &T instead of T?

时光怂恿深爱的人放手 提交于 2020-01-19 14:11:13

问题


use generic_array::*; // 0.12.3
use num::{Float, Zero}; // 0.2.0

#[derive(Clone, Debug)]
struct Vector<T, N: ArrayLength<T>> {
    data: GenericArray<T, N>,
}

impl<T, N: ArrayLength<T>> Vector<T, N>
where
    T: Float + Zero,
{
    fn dot(&self, other: Self) -> T {
        self.data
            .iter()
            .zip(other.data.iter())
            .fold(T::zero(), |acc, x| acc + *x.0 * *x.1)
    }

    fn length_sq(&self) -> T {
        self.dot(self.clone())
    }
}
error[E0308]: mismatched types
  --> src/lib.rs:21:18
   |
21 |         self.dot(self.clone())
   |                  ^^^^^^^^^^^^ expected struct `Vector`, found reference
   |
   = note: expected type `Vector<T, N>`
              found type `&Vector<T, N>`

Why does this happen? Why does clone return &T instead of T?

Why does this work if I implement Clone myself?

use generic_array::*; // 0.12.3
use num::{Float, Zero}; // 0.2.0

#[derive(Debug)]
struct Vector<T, N: ArrayLength<T>> {
    data: GenericArray<T, N>,
}

impl<T: Float, N: ArrayLength<T>> Clone for Vector<T, N> {
    fn clone(&self) -> Self {
        Vector::<T, N> {
            data: self.data.clone(),
        }
    }
}

impl<T, N: ArrayLength<T>> Vector<T, N>
where
    T: Float + Zero,
{
    fn dot(&self, other: Self) -> T {
        self.data
            .iter()
            .zip(other.data.iter())
            .fold(T::zero(), |acc, x| acc + *x.0 * *x.1)
    }

    fn length_sq(&self) -> T {
        self.dot(self.clone())
    }
}

回答1:


You get this error when your type doesn't implement Clone:

struct Example;

fn by_value(_: Example) {}

fn by_reference(v: &Example) {
    by_value(v.clone())
}
error[E0308]: mismatched types
 --> src/lib.rs:6:14
  |
6 |     by_value(v.clone())
  |              ^^^^^^^^^ expected struct `Example`, found &Example
  |
  = note: expected type `Example`
             found type `&Example`

This is due to the auto-referencing rules: the compiler sees that Example doesn't implement Clone, so it instead tries to use Clone on &Example, and immutable references always implement Clone.

The reason your Vector type doesn't implement Clone is because the derived Clone implementation doesn't have the right bounds on the type parameters (Rust issue #26925). Try explicitly writing self.dot(Self::clone(self)) to get an error message along these lines.

See also:

  • Deriving a trait results in unexpected compiler error, but the manual implementation works


来源:https://stackoverflow.com/questions/37765586/why-does-cloning-my-custom-type-result-in-t-instead-of-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!