update at 2021-11-23 18:32:43 by ehlxr
This commit is contained in:
parent
39bb6127de
commit
d25398b3bf
88
src/main.rs
88
src/main.rs
@ -1,10 +1,13 @@
|
|||||||
|
mod sub_mod;
|
||||||
|
mod user;
|
||||||
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::rc::{Rc, Weak};
|
use std::rc::{Rc, Weak};
|
||||||
use std::sync::{mpsc, Arc, Mutex};
|
use std::sync::{mpsc, Arc, Mutex};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::{collections::HashMap, fmt::Display, fs, fs::File, io, io::ErrorKind, thread};
|
use std::{collections::HashMap, fmt::Display, fs::File, io, io::ErrorKind, thread};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut s = String::from("Hello");
|
let mut s = String::from("Hello");
|
||||||
@ -87,8 +90,8 @@ fn main() {
|
|||||||
println!("----------------------------------------");
|
println!("----------------------------------------");
|
||||||
let my_str = String::from("hello world!");
|
let my_str = String::from("hello world!");
|
||||||
// let my_str = "hello world!";
|
// let my_str = "hello world!";
|
||||||
println!("{}, {}", my_str, first_words(&my_str[..]));
|
println!("{}, {}", my_str, sub_mod::first_words(&my_str[..]));
|
||||||
println!("get first word: {}", first_words("hello world"));
|
println!("get first word: {}", sub_mod::first_words("hello world"));
|
||||||
|
|
||||||
let rect = Rectangle {
|
let rect = Rectangle {
|
||||||
width: 21,
|
width: 21,
|
||||||
@ -293,7 +296,7 @@ fn main() {
|
|||||||
d(&mut s);
|
d(&mut s);
|
||||||
println!("{}", s);
|
println!("{}", s);
|
||||||
|
|
||||||
generate_workout(10, 3);
|
sub_mod::generate_workout(10, 3);
|
||||||
|
|
||||||
// ---------------------------
|
// ---------------------------
|
||||||
println!("----------------------------------------");
|
println!("----------------------------------------");
|
||||||
@ -602,7 +605,7 @@ fn main() {
|
|||||||
|
|
||||||
let x: u32 = 5;
|
let x: u32 = 5;
|
||||||
let point = (3, x as i32);
|
let point = (3, x as i32);
|
||||||
print_coordinates(&point);
|
sub_mod::print_coordinates(&point);
|
||||||
|
|
||||||
// ---------------------------
|
// ---------------------------
|
||||||
println!("---------------------------");
|
println!("---------------------------");
|
||||||
@ -812,7 +815,7 @@ fn main() {
|
|||||||
let mut v = vec![1, 2, 3, 4, 5, 6];
|
let mut v = vec![1, 2, 3, 4, 5, 6];
|
||||||
let r = &mut v[..];
|
let r = &mut v[..];
|
||||||
let (_a, _b) = r.split_at_mut(3);
|
let (_a, _b) = r.split_at_mut(3);
|
||||||
let (a, b) = split_at_mut(r, 3);
|
let (a, b) = sub_mod::split_at_mut(r, 3);
|
||||||
println!("{:?}", a);
|
println!("{:?}", a);
|
||||||
println!("{:?}", b);
|
println!("{:?}", b);
|
||||||
|
|
||||||
@ -935,7 +938,7 @@ fn main() {
|
|||||||
|
|
||||||
// Pancakes::hello_macro();
|
// Pancakes::hello_macro();
|
||||||
|
|
||||||
let user1 = User {
|
let user1 = user::User {
|
||||||
username: String::from("hllo"),
|
username: String::from("hllo"),
|
||||||
email: String::from("hell@world.com"),
|
email: String::from("hell@world.com"),
|
||||||
sign_in_count: 1,
|
sign_in_count: 1,
|
||||||
@ -1105,14 +1108,6 @@ fn test_tuple(t: (i32, &str)) {
|
|||||||
println!("{} {}", t.0, t.1);
|
println!("{} {}", t.0, t.1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct User {
|
|
||||||
username: String,
|
|
||||||
email: String,
|
|
||||||
sign_in_count: u64,
|
|
||||||
active: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
static mut COUNTER: u32 = 0;
|
static mut COUNTER: u32 = 0;
|
||||||
|
|
||||||
fn add_to_count(inc: u32) {
|
fn add_to_count(inc: u32) {
|
||||||
@ -1124,29 +1119,6 @@ fn add_to_count(inc: u32) {
|
|||||||
use core::fmt;
|
use core::fmt;
|
||||||
use std::fmt::Formatter;
|
use std::fmt::Formatter;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
use std::slice;
|
|
||||||
|
|
||||||
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),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn print_coordinates(&(x, y): &(i32, i32)) {
|
|
||||||
println!("Current location: ({}, {})", x, y);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Bt {
|
struct Bt {
|
||||||
@ -1284,18 +1256,6 @@ impl Message {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//fn first_words<'a>(s: &'a str) -> &'a str {
|
|
||||||
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[..]
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Rectangle {
|
struct Rectangle {
|
||||||
width: u32,
|
width: u32,
|
||||||
@ -1573,34 +1533,6 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Shoe {
|
struct Shoe {
|
||||||
size: u32,
|
size: u32,
|
||||||
|
65
src/sub_mod.rs
Normal file
65
src/sub_mod.rs
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
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);
|
||||||
|
}
|
7
src/user.rs
Normal file
7
src/user.rs
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#[derive(Debug)]
|
||||||
|
pub struct User {
|
||||||
|
pub username: String,
|
||||||
|
pub email: String,
|
||||||
|
pub sign_in_count: u64,
|
||||||
|
pub active: bool,
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user