fork download
  1. use std::io;
  2. use std::str::FromStr;
  3. use std::cmp::Ordering;
  4.  
  5. fn get_number(prompt_input: &str) -> u32 {
  6.  
  7. println!("{}", prompt_input);
  8.  
  9. let mut input = String::new();
  10.  
  11. io::stdin().read_line(&mut input).expect("no input!");
  12.  
  13. u32::from_str(input.trim()).unwrap()
  14. }
  15.  
  16. fn main() {
  17.  
  18. let a = get_number("Enter first number a = ");
  19.  
  20. let b = get_number("Enter first number b = ");
  21.  
  22. println!("The Greatest Common Divisor of {} and {} is {}", a, b, euclid( a, b) );
  23. }
  24.  
  25. fn euclid(a: u32, b: u32) -> u32 {
  26.  
  27. assert!(a > 0 && b > 0);
  28.  
  29. match a.cmp(&b) {
  30.  
  31. Ordering::Equal => b,
  32.  
  33. Ordering::Less => euclid(a,b-a),
  34.  
  35. Ordering::Greater => euclid(a-b,b)
  36. }
  37. }
Success #stdin #stdout 0s 5308KB
stdin
10
3
stdout
Enter first number a = 
Enter first number b = 
The Greatest Common Divisor of 10 and 3 is 1