fn main() {
let (mut initialEnergy, mut initialExperience) = (1, 1);
let energy:Vec<i32> = vec![1,1,1,1];
let experience:Vec<i32> = vec![1,1,1,50];
let res = min_number_of_hours(initialEnergy, initialExperience, &energy, &experience);
println!("res is: {}", res);
}
fn min_number_of_hours(initialEnergy:i32, mut initialExperience:i32, energy:&Vec<i32>, experience:&Vec<i32>) -> i32{
let mut trainingHours = if initialEnergy > sum {0} else { sum - initialEnergy + 1 };
for i in experience{
if initialExperience > *i{
initialExperience += i;
}else {
trainingHours += i - initialExperience + 1;
initialExperience = 2 * i + 1;
}
}
trainingHours
}
I've a hard time understanding your problem.
Do you want to know why you have to dereference i
here (deduced by me from your title)?
I guess, the question was "Why I have to derefence in if
condition only, while all the rest lines(e.g, initialExperience += i;
) do not require manual dereferencing?"
Well, that'd be because i32
implements Add<&i32>
, the trait that implements the logic for the +
operator (and other traits implementing other mathematical operators like +=
and *
, see std::ops
module), but not PartialEq<&i32>
(the trait used by comparison operators like >
). Only PartialEq<i32>
is implemented for i32
. So you can add, add assign and multiply, etc. &i32
to a i32
value, but you can not compare &i32
with i32
.
4 Likes
Thank you so much
1 Like