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

master
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),
};
@ -1025,14 +1025,12 @@ fn main() {
// File::create(fnames).unwrap()
// }
Err(e) => match e.kind() {
ErrorKind::NotFound => {
match File::create(fnames) {
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),
}
// 2.
// let mut f = File::open("hello.txt")?;
// let f = File::open(path);
//
// let mut f = match f {
// Ok(file) => file,
// Err(error) => return Err(error),
// };
//
// let mut s = String::new();
// f.read_to_string(&mut s)?;
// Ok(s)
// match f.read_to_string(&mut s) {
// Ok(_) => Ok(s),
// Err(e) => Err(e),
// }
// 2.
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)?;
// 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,7 +1387,7 @@ impl ToString for Tweet {
//}
fn notify<T, U>(t: &T, u: &U) -> String
where
where
T: Summary + ToString,
U: Summary,
{
@ -1417,7 +1457,7 @@ fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
}
struct Cacher<T, D, F>
where
where
T: Fn(&D) -> F,
{
calculation: T,
@ -1425,7 +1465,7 @@ struct Cacher<T, D, F>
}
impl<T, D, F> Cacher<T, D, F>
where
where
D: Hash + Eq,
F: Copy,
T: Fn(&D) -> F,