Is there a better way to use i
and j
for-in variables that avoids the use of std::convert::TryInto
and return vec![i.try_into().unwrap(),j.try_into().unwrap()];
for dealing with the usize and i32 conversion problem between what is expected as a result and the actual value type of these variables?
The use of the module and the try_into()
and unwrap()
functions was because of the compiler error suggestion. But I want to know if there is another way to cast or convert numeric values.
use std::convert::TryInto;
impl Solution {
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
let mut current = 0;
for i in 0..nums.len() - 1 {
for j in 1..nums.len(){
if j != i {
current = nums[i] + nums[j];
if current == target {
return vec![i.try_into().unwrap(),j.try_into().unwrap()];
}
}
}
}
vec![]
}
}