2017-12-20 10 views
0

[]演算子を受け入れる任意のクラスを取ることができる関数を作成しようとしています。私はそれを受け入れることができるようにしたい:インデックス可能なデータ型を引数として受け入れる関数

  1. ベクターまたはいくつかは、私が見つかりました実験により

を索引付けすることができる任意の他の容器基準によってまたは値のいずれかによって

  • アレイPartialOrdPartialEqのようないくつかの他の形質も必要とすることがわかった。私はまた、コンテナ内のオブジェクトの数を調べる必要があります。それは、これらのエラーを生成

    use std::ops::Index; 
    use std::iter::ExactSizeIterator; 
    use std::cmp::PartialEq; 
    
    pub fn find<'a, I>(input: I, key: <I as std::ops::Index<u32>>::Output) -> Option<u32> 
    where 
        I: Index<u32> + ExactSizeIterator, 
        <I as std::ops::Index<u32>>::Output: PartialEq + std::marker::Sized + std::cmp::PartialOrd, 
    { 
        if input.len() == 0 { 
         return None; 
        } 
        if key < input[0] || key > input[(input.len() - 1) as u32] { 
         return None; 
        } 
        let mut index: u32 = (input.len() - 1) as u32; 
        loop { 
         if key < input[index] { 
          index /= 2; 
          continue; 
         } else if input[index] < key && input[index + 1] > key { 
          return None; 
         } else if key > input[index] { 
          index += index/2; 
          continue; 
         } else if input[index] == key { 
          return Some(index); 
         } else { 
          return None; 
         } 
        } 
    } 
    
    fn main() { 
        assert_eq!(find(&[1, 2], 2), Some(1)); 
        assert_eq!(find([1, 2], 2), Some(1)); 
        assert_eq!(find(vec![1, 2], 2), Some(1)); 
    } 
    

    は、ここに私のコードです

    error[E0277]: the trait bound `&[{integer}; 2]: std::ops::Index<u32>` is not satisfied 
        --> src/main.rs:36:16 
        | 
    36 |  assert_eq!(find(&[1, 2], 2), Some(1)); 
        |    ^^^^ the type `&[{integer}; 2]` cannot be indexed by `u32` 
        | 
        = help: the trait `std::ops::Index<u32>` is not implemented for `&[{integer}; 2]` 
        = note: required by `find` 
    
    error[E0277]: the trait bound `&[{integer}; 2]: std::iter::ExactSizeIterator` is not satisfied 
        --> src/main.rs:36:16 
        | 
    36 |  assert_eq!(find(&[1, 2], 2), Some(1)); 
        |    ^^^^ the trait `std::iter::ExactSizeIterator` is not implemented for `&[{integer}; 2]` 
        | 
        = note: required by `find` 
    
    error[E0277]: the trait bound `[{integer}; 2]: std::ops::Index<u32>` is not satisfied 
        --> src/main.rs:37:16 
        | 
    37 |  assert_eq!(find([1, 2], 2), Some(1)); 
        |    ^^^^ the type `[{integer}; 2]` cannot be indexed by `u32` 
        | 
        = help: the trait `std::ops::Index<u32>` is not implemented for `[{integer}; 2]` 
        = note: required by `find` 
    
    error[E0277]: the trait bound `[{integer}; 2]: std::iter::ExactSizeIterator` is not satisfied 
        --> src/main.rs:37:16 
        | 
    37 |  assert_eq!(find([1, 2], 2), Some(1)); 
        |    ^^^^ the trait `std::iter::ExactSizeIterator` is not implemented for `[{integer}; 2]` 
        | 
        = note: required by `find` 
    
    error[E0277]: the trait bound `std::vec::Vec<{integer}>: std::ops::Index<u32>` is not satisfied 
        --> src/main.rs:38:16 
        | 
    38 |  assert_eq!(find(vec![1, 2], 2), Some(1)); 
        |    ^^^^ the type `std::vec::Vec<{integer}>` cannot be indexed by `u32` 
        | 
        = help: the trait `std::ops::Index<u32>` is not implemented for `std::vec::Vec<{integer}>` 
        = note: required by `find` 
    
    error[E0277]: the trait bound `std::vec::Vec<{integer}>: std::iter::ExactSizeIterator` is not satisfied 
        --> src/main.rs:38:16 
        | 
    38 |  assert_eq!(find(vec![1, 2], 2), Some(1)); 
        |    ^^^^ the trait `std::iter::ExactSizeIterator` is not implemented for `std::vec::Vec<{integer}>` 
        | 
        = note: required by `find` 
    
  • +0

    コンパイルエラーはありませんが、上記の例で実行してみてください。 [6,7,8,3]または[1,2,3,4,8]の引数を使用して呼び出すようにしてください –

    +0

    適切な[MCVE]を書くようにしてください。 [Rust Playground](https://play.rust-lang.org)の正確な問題を再現することはプラスです。 –

    +0

    エラーテキストを読んで、エラーでコンパイラが間違っていると思われる理由を教えてください。たとえば、何かが実装されていないことがわかった場合、それはなぜだと思いますか?それ以外の場合は、エラーメッセージを意図的に無視しているように見えます。 – Shepmaster

    答えて

    0

    が、私はそれを実装する方法を発見したが、いくつかの制限と。

    pub struct Validator { 
        data: Vec<i32>, 
    } 
    impl<'a> From<&'a [i32; 2]> for Validator { 
        fn from(input: &'a [i32; 2]) -> Self { 
         Validator { 
          data: input.iter().map(|c| *c).collect(), 
         } 
        } 
    } 
    impl From<[i32; 2]> for Validator { 
        fn from(input: [i32; 2]) -> Self { 
         Validator { 
          data: input.iter().map(|c| *c).collect(), 
         } 
        } 
    } 
    impl From<Vec<i32>> for Validator { 
        fn from(input: Vec<i32>) -> Self { 
         Validator { data: input } 
        } 
    } 
    
    pub fn find<T>(input: T, key: i32) -> Option<usize> 
    where 
        T: std::convert::Into<Validator>, 
        Validator: std::convert::From<T>, 
    { 
        let input: Vec<i32> = input.into().data; 
    
        if input.len() == 0 { 
         return None; 
        } 
        if key < input[0] || key > input[(input.len() - 1)] { 
         return None; 
        } 
        let mut index = input.len() - 1; 
        loop { 
         if key < input[index] { 
          index /= 2; 
          continue; 
         } else if input[index] < key && input[index + 1] > key { 
          return None; 
         } else if key > input[index] { 
          index += index/2; 
          continue; 
         } else if input[index] == key { 
          return Some(index); 
         } else { 
          return None; 
         } 
        } 
    } 
    
    fn main() { 
        assert_eq!(find(&[1, 2], 2), Some(1)); 
        assert_eq!(find([1, 2], 2), Some(1)); 
        assert_eq!(find(vec![1, 2], 2), Some(1)); 
    } 
    

    3つ以上の数字の配列を受け入れる関数が必要な場合は、要素数ごとにそれを実装する必要があります。 VecDequeなどの構造体やカスタムバリデーターのサポートを追加するには、バリデーター構造体とサポートするタイプのためにInto特性を実装します。

    関連する問題