From efc57d28c80066937387f218d621ab7109cd4ecf Mon Sep 17 00:00:00 2001 From: ehlxr Date: Tue, 23 Jun 2020 18:08:26 +0800 Subject: [PATCH] update at 2020-06-23 18:08:26 by ehlxr --- hello2.txt | 1 + src/main.rs | 126 ++++++++++++++++++++++++++++++++++------------------ 2 files changed, 84 insertions(+), 43 deletions(-) diff --git a/hello2.txt b/hello2.txt index e69de29..4baeb37 100644 --- a/hello2.txt +++ b/hello2.txt @@ -0,0 +1 @@ +千万千万群群无群无群无群无群所多所多 \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index b506cf3..e119025 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 的强引用(strong_count)为 0,但是 Weak 弱引用数(weak_count)仍然为 1(引用 Weak 的 Rc 仍然存在), - // weak_count 无需计数为 0 就能使 Rc 实例被清理。 + // weak_count 无需计数为 0 就能使 Rc 实例被清理。 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 { + x: T, + y: U, +} + +impl Point1 { + fn x(&self) -> &D { + &self.x + } +} + +impl Point1 { + fn y(&self) -> i32 { + self.y + } +} + +impl Point1 { + fn mixup(self, p: Point1) -> Point1 { + Point1 { x: self.x, y: p.y } } } @@ -1278,36 +1318,36 @@ mod performance_group { } } -// 传播错误 -fn read_username_from_file() -> Result { +/// 传播错误 +fn read_username_from_file(path: &str) -> Result { // 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: &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 - where - T: Fn(&D) -> F, +where + T: Fn(&D) -> F, { calculation: T, value: HashMap, } impl Cacher - 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 { Self {