Skip to content

Instantly share code, notes, and snippets.

@da-moon
Last active September 7, 2018 17:39
Show Gist options
  • Save da-moon/d175894d27625f69713b1c8f4fe21cfb to your computer and use it in GitHub Desktop.
Save da-moon/d175894d27625f69713b1c8f4fe21cfb to your computer and use it in GitHub Desktop.
Rust basic Operatrions
// Concat a string inside a loop and trun it into a str
//https://users.rust-lang.org/t/how-do-i-avoid-temporary-value-created-here-errors-when-creating-an-array/9433/14
let mut result: Vec<String> = Vec::new();
let mut counter = 0;
while counter < list.len() {
if counter+1< list.len(){
result.push(format!("For want of a {} the {} was lost.\n", list[counter], list[counter+1]));
}
if counter+1 == list.len(){
result.push(format!("For want of a {} the {} was lost.\n", list[counter-1], list[counter]));
}
counter +=1;
}
let result = result.iter().map(|s| s.as_str()).collect::<Vec<_>>().join("\n");
pub fn three_layer_nested_loop() {
let sum_result = 12 ;
(1..sum_result)
.flat_map(|a| (1..=(sum_result - a))
.map(move |b| (a, b) ))
.flat_map(move|a_and_b| (1..=(sum_result - (a_and_b.0+a_and_b.1)))
.map(move|c| (a_and_b.0,a_and_b.1, c) ))
.filter(move |&(a, b, c)| ((a, b, c).0 + (a, b, c).1 + (a, b, c).2 == sum_result ) && ((a, b, c).0 * (a, b, c).0 + (a, b, c).1 * (a, b, c).1 == (a, b, c).2 * (a, b, c).2) )
.for_each(| filtered_number| {
println!("a = {:#?}\n", filtered_number.0);
println!("b = {:#?}\n", filtered_number.1);
println!("c = {:#?} \n", filtered_number.2);
println!("=======================\n");
});
}
let res = format!("F... {} ... {} ...\n", x,y);
let counter : u32 = 3;
let i : Option<u32> = counter.checked_sub(1);
if i.is_none() == false{
let temp = i.unwrap();
}
let expected = vec![
"For want of a nail the shoe was lost.",
"And all for the want of a nail.",
].join("\n");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment