Clone
がバインドされていない限り、汎用タイプのオブジェクトへの参照を含む構造体のClone
の特性を導出すると、オブジェクトへの参照を返しますが新しいオブジェクトではないclone()
メソッドを生成します。派生したclone()メソッドが参照を返すのはなぜですか?
#[derive(Clone)]
struct A<'a, T: 'a>{
ref_generic: &'a T
}
fn test_call<'a, T: 'a>(a: &A<'a, T>)->A<'a, T>{
a.clone()
}
エラーが発生します:
error[E0308]: mismatched types
--> src/lib.rs:15:5
|
14 | fn test_call<'a, T: 'a>(a: &A<'a, T>)->A<'a, T>{
| -------- expected `A<'a, T>` because of return type
15 | a.clone()
| ^^^^^^^^^ expected struct `A`, found &A<'_, T>
|
= note: expected type `A<'a, T>`
found type `&A<'_, T>`
はなぜ、このように動作導出んが
私は、コードを持っていますか?手動で実装すると、この障害は回避できますが、不愉快です。
impl<'a, T: 'a> Clone for A<'a, T>{
fn clone(&self)->Self{
A{ref_generic: self.ref_generic}
}
}
これは、 'struct A <'a、T:Clone +' a>'、 'fn test_call <'a, T: 'a>'を 'fn test_call <'a、T:Clone +' a > '、あなたの例は動作します。 –