add single file tests
This commit is contained in:
26
single_file_tests/time.rs
Normal file
26
single_file_tests/time.rs
Normal 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);
|
||||
}
|
||||
25
single_file_tests/vec_test.rs
Normal file
25
single_file_tests/vec_test.rs
Normal 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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user