use std::env;
use std::str::FromStr;
fn main() {
/*
// Build a vector of the strings "101", "102", ... "105"
let mut v = Vec::new();
for i in 101 .. 106 {
v.push(i.to_string());
}
for i in 0..v.len() {
println!("循环数值[{}]={}", i, v[i]);
}
// 1. Pop a value off the end of the vector:
let fifth = v.pop().expect("vector empty!");
assert_eq!(fifth, "105");
// 2. Move a value out of a given index in the vector,
// and move the last element into its spot:
let second = v.swap_remove(1);
assert_eq!(second, "102");
for i in 0..v.len() {
println!("循环数值2[{}]={}", i, v[i]);
}
// 3. Swap in another value for the one we're taking out:
let third = std::mem::replace(&mut v[2], "substitute".to_string());
// assert_eq!(third, "103");
for i in 0..v.len() {
println!("循环数值---3[{}]={}", i, v[i]);
}
// Let's see what's left of our vector.
assert_eq!(v, vec!["101", "104", "substitute"]);
*/
/*
let v = vec!["liberté".to_string(),
"égalité".to_string(),
"fraternité".to_string()];
for mut s in v {
s.push('8');
println!("{}", s);
}
*/
/*
struct Person { name: Option<String>, birth: i32 }
let mut composers = Vec::new();
composers.push(Person { name: Some("Palestrina".to_string()),
birth: 1525 });
//let first_name = composers[0].name;
let first_name = composers[0].name.take();//等于std::mem::replace(&mut composers[0].name, Some("dsfd".to_string()));
//let first_name = std::mem::replace(&mut composers[0].name, Some("dsfd".to_string()));
assert_eq!(first_name, Some("Palestrina".to_string()));
assert_eq!(composers[0].name, None);
fn divide(numerator: f64, denominator: f64) -> Option<f64> {
if denominator == 0.0 {
None
} else {
Some(numerator / denominator)
}
}
// 函数的返回值是一个选项
let result = divide(2.0, 2.0);
// 模式匹配以获取值
match result {
// 该划分有效
Some(x) => println!("Result: {}", x),
// 划分无效
None => println!("Cannot divide by 0"),
}
*/
test_gcd();
let mut numbers = Vec::new();
for arg in env::args().skip(1) {
numbers.push(u64::from_str(&arg).expect("error parsing argument"));
}
if numbers.len() == 0 {
eprintln!("Usage: gcd NUMBER ...");
std::process::exit(1);
}
let mut d = numbers[0];
for m in &numbers[1..] {
d = gcd(d, *m);
}
println!("The greatest common divisor of {:?} is {}", numbers, d);
}
fn test_gcd() {
assert_eq!(gcd(56, 42), 1);
assert_eq!(gcd(2 * 3 * 5 * 11 * 17, 3 * 7 * 11 * 13 * 19), 3 * 11);
}
fn gcd(mut n: u64, mut m: u64) -> u64 {
assert!(n != 0 && m != 0);
while m != 0 {
if m < n {
let t = m;
m = n;
n = t;
}
m = m % n;
}
n
}