Skip to content

Instantly share code, notes, and snippets.

@dns2utf8
Last active October 9, 2018 21:37
Show Gist options
  • Save dns2utf8/11b541f0b4165f1cc39472c15f494a00 to your computer and use it in GitHub Desktop.
Save dns2utf8/11b541f0b4165f1cc39472c15f494a00 to your computer and use it in GitHub Desktop.
/// Source: https://gist.github.com/dns2utf8/11b541f0b4165f1cc39472c15f494a00
fn main() {
let mut result = 0;
let list = vec![-8, 30, -42, 32];
println!("init result = {}!", result);
println!("init list = {:?}!", list);
absolute_max(&mut result, &list);
println!("final result = {}!", result);
}
fn absolute_max(result: &mut i64, list: &Vec<i64>) {
for i in list {
let i = *i;
let result_local = *result;
let abs_i = if i < 0 { -i } else { i };
let abs_r = if result_local < 0 { -result_local } else { result_local };
if abs_r < abs_i {
*result = i;
}
}
}
@dns2utf8
Copy link
Author

dns2utf8 commented Oct 9, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment