704 Binary Search (step 1) error for Rust program is confusing
167

My program will not pass because of a compile time error:

Line 7, Char 24: type annotations needed (solution.rs)
   |
7  |             if nums[i] == target.try_into().unwrap()
   |                        ^^ cannot infer type for type `i32`
8  |          {
9 |             return i.try_into().unwrap()
   |                    ------------ this method call resolves to `std::result::Result<T, <Self as std::convert::TryInto<T>>::Error>`
   |
   = note: cannot satisfy `i32: std::cmp::PartialEq<_>`
error: aborting due to previous error

Anyone familar with Rust know how to correct this? It runs fine using cargo on my machine and I've been told it is correct but probably not the proper way to do it so I don't know. I'd like some advice on this.

use std::convert::TryInto;

struct Solution;
impl Solution 
{
    fn search(nums: Vec<i32>, target: i32) -> i32 {
        for i in 0..nums.len(){
            if nums[i] == target.try_into().unwrap() 
			{
				return i.try_into().unwrap()
			}
        }
		-1
    }
}

fn main() {
    let nums = vec![-1, 0, 3, 5, 9, 12];
    let target = 9;
    let my_search = Solution::search(nums, target);
	println!("{}", my_search)
}
Comments (3)