Last active
October 9, 2018 21:37
-
-
Save dns2utf8/11b541f0b4165f1cc39472c15f494a00 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run on play.rust-lang.org