rust_study/src/sub_mod.rs

66 lines
1.7 KiB
Rust

use std::{slice, thread, time::Duration};
use crate::Cacher;
pub fn split_at_mut(slice: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) {
// let len = slice.len();
// assert!(mid <= len);
// (&mut slice[..mid], &mut slice[mid..])
let len = slice.len();
let ptr = slice.as_mut_ptr();
assert!(mid <= len);
unsafe {
(
slice::from_raw_parts_mut(ptr, mid),
slice::from_raw_parts_mut(ptr.offset(mid as isize), len - mid),
)
}
}
//pub fn first_words<'a>(s: &'a str) -> &'a str {
pub fn first_words(s: &str) -> &str {
let b = s.as_bytes();
for (i, &item) in b.iter().enumerate() {
if item == b' ' {
return &s[0..i];
}
}
&s[..]
}
pub fn generate_workout(intensity: u32, random_number: u32) {
// let calculation = |num: u32| -> u32 {
// println!("calculating slowly...");
// thread::sleep(Duration::from_secs(2));
// num
// };
let mut expensive_result = Cacher::new(|num| {
println!("calculating slowly...");
thread::sleep(Duration::from_millis(500));
num + 1
});
if intensity < 25 {
println!("Today, do {} pushups!", expensive_result.value(intensity));
println!("Next, do {} situps!", expensive_result.value(intensity));
} else {
if random_number == 3 {
println!("Take a break today! Remember to stay hydrated!");
} else {
println!(
"Today, run for {} minutes!",
expensive_result.value(intensity)
);
}
}
}
pub fn print_coordinates(&(x, y): &(i32, i32)) {
println!("Current location: ({}, {})", x, y);
}