It's taking 2.4 MB of memory and 20 ms. It's my solution for Two Sum problem on LeetCode. How can I make it better using closures and other stuff? Kindly review the code.
use std::convert::TryInto;
impl Solution {
pub fn two_sum(nums: Vec<i32>, target: i32) ->Vec<i32> {
let mut status:bool = false;
let mut result:Vec<i32> = Vec::with_capacity(2);
let rang = nums.len()-1;
if (nums.len() < 2 || nums.len() > 10_0000) && (target < -1000000000 || target > 1000000000){
panic!("Too few or too much values");
}else{
'outer: for (i, val) in nums.iter().enumerate() {
if nums[i] < -1000000000 || nums[i] > 1000000000{
panic!("Too large or too small value in vec");
}
'inner: for j in i+1..nums.len() {
// println!("{}", nums[i]);
if nums[i] + nums[j] == target {
// println!("Hanji paaji mil gye ney..");
status = true;
// result[0] = x.try_into().unwrap();
// result[1] = (x+1).try_into().unwrap();
result.push(i.try_into().unwrap());
result.push((j).try_into().unwrap());
// result
break 'outer;
}else{
continue;
}
}
}
}
if status{
result
}else{
panic!("Not found");
}
// result
}
}