add single file tests

This commit is contained in:
2020-01-30 21:14:00 +08:00
parent 44d6207f6a
commit 85b24c3ac6
2 changed files with 51 additions and 0 deletions

26
single_file_tests/time.rs Normal file
View File

@@ -0,0 +1,26 @@
use std::time::SystemTime;
fn get_year_and_left_secs(secs_from_1970: u64) -> (u32, u64) {
let mut t = secs_from_1970;
let mut y = 1970;
loop {
let is_leap_year = y % 4 == 0 && y % 100 != 0;
let secs_per_year = if is_leap_year { 366 } else { 365 } * 24 * 60 * 60;
if t > secs_per_year {
y += 1;
t -= secs_per_year;
} else {
return (y, t);
}
}
}
fn main() {
let secs_from_1970 = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
let (year_gmt, _) = get_year_and_left_secs(secs_from_1970);
let (year_gmt8, _) = get_year_and_left_secs(secs_from_1970 + 8 * 60 * 60);
println!("Secs from 1970: {}", secs_from_1970);
println!("Year (GMT) : {}", year_gmt);
println!("Year (GMT+8) : {}", year_gmt8);
}

View File

@@ -0,0 +1,25 @@
fn main() {
let v = vec![1, 3, 5];
println!("For each print:");
for x in &v {
println!("{}", x);
}
println!("Enumerate for each print:");
for (i, x) in v.iter().enumerate() {
println!("{} -> {}", i, x);
}
println!("Pop print:");
let mut v2 = vec![2, 4, 6];
while let Some(x) = v2.pop() {
println!("{}", x);
}
println!("Map:");
let v3: Vec<i32> = (0..3).collect();
let v4 = v3.iter().map(|&x| x * 2).collect::<Vec<_>>();
println!("{:?} -> {:?}", v3, v4);
}