2015-01-06 6 views
7

汎用イテレータのイテレータの出力のタイプに境界を指定する方法がわかりません。Iterator :: Itemの型をバインドする方法は?

fn somefunc<A: Int, I: Iterator<A>>(xs: I) { 
    xs.next().unwrap().pow(2); 
} 

をしかし、今、私はイテレータのItemタイプに境界を置くするかどうかはわかりません:錆1.0前に、私はこれを行うことができるために使用されます。

fn somefunc<I: Iterator>(xs: I) { 
    xs.next().unwrap().pow(2); 
} 
error: no method named `pow` found for type `<I as std::iter::Iterator>::Item` in the current scope 
--> src/main.rs:2:28 
    | 
2 |   xs.next().unwrap().pow(2); 
    |       ^^^ 

は、どのように私はこの仕事を得ることができますか?

答えて

11

あなたは二ジェネリック型パラメータを導入し、その上に結合を配置することができます:

fn somefunc<A: Int, I: Iterator<Item = A>>(mut xs: I) { 
    xs.next().unwrap().pow(2); 
} 

ます。また、関連する型自体

fn somefunc<I: Iterator>(mut xs: I) 
where 
    I::Item: Int, 
{ 
    xs.next().unwrap().pow(2); 
} 
に形質境界を配置することができます
関連する問題