実現したいこと
Cloneトレイトを実装するイテレータを返すメソッドを提供するトレイトを実装したいと考えて次のようなコードを書きました。
重要な部分は次のトレイトです。関連型Iter
に対してIterator<&'a T> + Clone
という制約をかけています。
trait IterFactory<'a, T: 'a> { type Iter: Iterator<Item=&'a T> + Clone; fn iter(&'a self) -> Self::Iter; }
エラーメッセージと質問
上記のplaygroundに置いたコードをコンパイルすると以下のエラーが出ます。
error[E0277]: the trait bound `T: std::clone::Clone` is not satisfied --> src/main.rs:29:17 | 29 | impl<'a, T: 'a> IterFactory<'a, T> for Container<T> { | ^^^^^^^^^^^^^^^^^^ the trait `std::clone::Clone` is not implemented for `T` | = help: consider adding a `where T: std::clone::Clone` bound = note: required because of the requirements on the impl of `std::clone::Clone` for `ContainerIter<'_, T>`
コンパイラがT
に対してもClone
を実装することを要求していますが、私が実装したいイテレータはT
の参照を返すのでClone
はいらないはずです。
これはコンパイラの正しい挙動なのかどうか、このエラーを回避する方法はないかを知りたいと思っています。よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/27 07:40