update at 2020-06-23 18:08:26 by ehlxr

This commit is contained in:
ehlxr 2020-06-23 18:08:26 +08:00
parent 26ab99279a
commit efc57d28c8
2 changed files with 84 additions and 43 deletions

View File

@ -0,0 +1 @@
千万千万群群无群无群无群无群所多所多

View File

@ -4,7 +4,7 @@ use std::ops::Deref;
use std::rc::{Rc, Weak};
use std::sync::{mpsc, Arc, Mutex};
use std::time::Duration;
use std::{collections::HashMap, fmt::Display, fs::File, io, io::ErrorKind, thread};
use std::{collections::HashMap, fmt::Display, fs, fs::File, io, io::ErrorKind, thread};
fn main() {
let mut s = String::from("Hello");
@ -216,7 +216,7 @@ fn main() {
};
println!("{:?}", f.metadata());
match read_username_from_file() {
match read_username_from_file("hello2.txt") {
Ok(st) => println!("{}", st),
Err(err) => panic!("There was a problem read string: {:?}", err),
};
@ -409,7 +409,7 @@ fn main() {
leaf.parent.borrow().upgrade(),
);
} // -> 离开作用域此时Rc<branch> 的强引用strong_count为 0但是 Weak<branch> 弱引用数weak_count仍然为 1引用 Weak<branch> 的 Rc<leaf> 仍然存在),
// weak_count 无需计数为 0 就能使 Rc<branch> 实例被清理。
// weak_count 无需计数为 0 就能使 Rc<branch> 实例被清理。
println!(
"leaf strong = {}, weak = {}, parent = {:?}",
Rc::strong_count(&leaf),
@ -948,7 +948,7 @@ fn main() {
let s1 = String::from("Hello, ");
let s2 = String::from("World!");
let s3 = s1 + &s2; // 注意 s1 被移动了,不能继续使用
// println!("{} {} {}", s1, s3, s2);
// println!("{} {} {}", s1, s3, s2);
println!("{} {}", s3, s2);
let hello = "测试中文字符串";
@ -1025,14 +1025,12 @@ fn main() {
// File::create(fnames).unwrap()
// }
Err(e) => match e.kind() {
ErrorKind::NotFound => {
match File::create(fnames) {
Ok(f) => f,
Err(e) => panic!("Problem creating the file: {:?}", e),
}
}
ErrorKind::NotFound => match File::create(fnames) {
Ok(f) => f,
Err(e) => panic!("Problem creating the file: {:?}", e),
},
oe => panic!("Problem opening the file: {:?}", oe),
}
},
};
println!("{:?}", file);
@ -1040,7 +1038,49 @@ fn main() {
let m = Message::Quit;
match m {
Message::Write(s) => println!("{}", s),
// 此处可使用任意字符匹配其它情况
abc => println!("{:?}", abc),
};
println!("----------------错误传递--------------");
let r = read_username_from_file("hello2.txt");
match r {
Ok(s) => println!("read from file result: {}", s),
Err(e) => println!("read from file error: {:?}", e),
};
println!("--------------泛型--------------");
let p = Point1 { x: 5, y: 10 };
println!("p.x = {}", p.x());
println!("p.y = {}", p.y());
let p1 = Point1 { x: "hello", y: 'd' };
let p2 = p.mixup(p1);
println!("{:?}", p2);
}
#[derive(Debug)]
struct Point1<T, U> {
x: T,
y: U,
}
impl<D, Z> Point1<D, Z> {
fn x(&self) -> &D {
&self.x
}
}
impl Point1<i32, i32> {
fn y(&self) -> i32 {
self.y
}
}
impl<T, U> Point1<T, U> {
fn mixup<V, W>(self, p: Point1<V, W>) -> Point1<T, W> {
Point1 { x: self.x, y: p.y }
}
}
@ -1278,36 +1318,36 @@ mod performance_group {
}
}
// 传播错误
fn read_username_from_file() -> Result<String, io::Error> {
/// 传播错误
fn read_username_from_file(path: &str) -> Result<String, io::Error> {
// 1.
let f = File::open("hello.txt");
let mut f = match f {
Ok(file) => file,
Err(error) => return Err(error),
};
let mut s = String::new();
match f.read_to_string(&mut s) {
Ok(_) => Ok(s),
Err(e) => Err(e),
}
// let f = File::open(path);
//
// let mut f = match f {
// Ok(file) => file,
// Err(error) => return Err(error),
// };
//
// let mut s = String::new();
// match f.read_to_string(&mut s) {
// Ok(_) => Ok(s),
// Err(e) => Err(e),
// }
// 2.
// let mut f = File::open("hello.txt")?;
//
// let mut s = String::new();
// f.read_to_string(&mut s)?;
// Ok(s)
let mut f = File::open(path)?;
let mut s = String::new();
f.read_to_string(&mut s)?;
Ok(s)
// 3.
// let mut s = String::new();
// File::open("hello.txt")?.read_to_string(&mut s)?;
// Ok(s)
// let mut s = String::new();
// File::open(path)?.read_to_string(&mut s)?;
// Ok(s)
// 4.
// fs::read_to_string("hello.txt")
// fs::read_to_string(path)
}
trait Summary {
@ -1347,9 +1387,9 @@ impl ToString for Tweet {
//}
fn notify<T, U>(t: &T, u: &U) -> String
where
T: Summary + ToString,
U: Summary,
where
T: Summary + ToString,
U: Summary,
{
format!("{}, {}", u.summarize(), t.to_string())
}
@ -1417,18 +1457,18 @@ fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
}
struct Cacher<T, D, F>
where
T: Fn(&D) -> F,
where
T: Fn(&D) -> F,
{
calculation: T,
value: HashMap<D, F>,
}
impl<T, D, F> Cacher<T, D, F>
where
D: Hash + Eq,
F: Copy,
T: Fn(&D) -> F,
where
D: Hash + Eq,
F: Copy,
T: Fn(&D) -> F,
{
fn new(calculation: T) -> Cacher<T, D, F> {
Self {