damoonazarpazhooh
9/6/2018 - 1:31 PM

Rust basic Operatrions

Rust basic Operatrions

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 counter : u32 = 3;
let i : Option<u32> = counter.checked_sub(1);
if i.is_none() == false{
  let temp = i.unwrap();
}
let res = format!("F... {} ... {} ...\n", x,y);
let expected = vec![
    "For want of a nail the shoe was lost.",
    "And all for the want of a nail.",
].join("\n");
// 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");