update at 2020-06-21 19:00:26 by ehlxr
This commit is contained in:
parent
939f409503
commit
26ab99279a
52
Cargo.lock
generated
52
Cargo.lock
generated
@ -1,58 +1,6 @@
|
|||||||
# This file is automatically @generated by Cargo.
|
# This file is automatically @generated by Cargo.
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
[[package]]
|
|
||||||
name = "hello_macro"
|
|
||||||
version = "0.1.0"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "hello_macro_derive"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
"syn 0.14.9 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "proc-macro2"
|
|
||||||
version = "0.4.30"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
dependencies = [
|
|
||||||
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "quote"
|
|
||||||
version = "0.6.13"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rust_study"
|
name = "rust_study"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
|
||||||
"hello_macro 0.1.0",
|
|
||||||
"hello_macro_derive 0.1.0",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "syn"
|
|
||||||
version = "0.14.9"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
"quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
"unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "unicode-xid"
|
|
||||||
version = "0.1.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
|
|
||||||
[metadata]
|
|
||||||
"checksum proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)" = "cf3d2011ab5c909338f7887f4fc896d35932e29146c12c8d01da6b22a80ba759"
|
|
||||||
"checksum quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "6ce23b6b870e8f94f81fb0a363d65d86675884b34a09043c81e5562f11c1f8e1"
|
|
||||||
"checksum syn 0.14.9 (registry+https://github.com/rust-lang/crates.io-index)" = "261ae9ecaa397c42b960649561949d69311f08eeaea86a65696e6e46517cf741"
|
|
||||||
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
|
|
||||||
|
@ -5,5 +5,5 @@ authors = ["ehlxr <ehlxr.me@gmail.com>"]
|
|||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
hello_macro = { path = "../hello_macro" }
|
# hello_macro = { path = "../hello_macro" }
|
||||||
hello_macro_derive = { path = "../hello_macro/hello_macro_derive" }
|
# hello_macro_derive = { path = "../hello_macro/hello_macro_derive" }
|
0
hello2.txt
Normal file
0
hello2.txt
Normal file
97
src/main.rs
97
src/main.rs
@ -917,13 +917,13 @@ fn main() {
|
|||||||
|
|
||||||
// ---------------------------
|
// ---------------------------
|
||||||
println!("---------------------------");
|
println!("---------------------------");
|
||||||
use hello_macro::HelloMacro;
|
// use hello_macro::HelloMacro;
|
||||||
use hello_macro_derive::HelloMacro;
|
// use hello_macro_derive::HelloMacro;
|
||||||
|
|
||||||
#[derive(HelloMacro)]
|
// #[derive(HelloMacro)]
|
||||||
struct Pancakes;
|
// struct Pancakes;
|
||||||
|
|
||||||
Pancakes::hello_macro();
|
// Pancakes::hello_macro();
|
||||||
|
|
||||||
let user1 = User {
|
let user1 = User {
|
||||||
username: String::from("hllo"),
|
username: String::from("hllo"),
|
||||||
@ -962,7 +962,86 @@ fn main() {
|
|||||||
println!("");
|
println!("");
|
||||||
// let s = hello[1]; // Rust 的字符串不支持索引
|
// let s = hello[1]; // Rust 的字符串不支持索引
|
||||||
// let s = &hello[0..4]; // thread 'main' panicked at 'byte index 4 is not a char boundary; it is inside '试' (bytes 3..6) of `测试中文字符串`', src/libcore/str/mod.rs:2219:5
|
// let s = &hello[0..4]; // thread 'main' panicked at 'byte index 4 is not a char boundary; it is inside '试' (bytes 3..6) of `测试中文字符串`', src/libcore/str/mod.rs:2219:5
|
||||||
print!("{}", &hello[0..3]);
|
println!("{}", &hello[0..3]);
|
||||||
|
|
||||||
|
println!("--------------HashMap-----------");
|
||||||
|
|
||||||
|
let mut scores = HashMap::new();
|
||||||
|
scores.insert(String::from("Blue"), 10);
|
||||||
|
scores.insert(String::from("Yellow"), 50);
|
||||||
|
println!("{:?}", scores);
|
||||||
|
for (k, v) in &scores {
|
||||||
|
println!("k {}, v {}", k, v);
|
||||||
|
}
|
||||||
|
// 只在键没有对应值时插入
|
||||||
|
// Entry 的 or_insert 方法在键对应的值存在时就返回这个值的 Entry ,
|
||||||
|
// 如果不存在则将参数作为新值插入并返回修改过的 Entry
|
||||||
|
let x3 = scores.entry("Blue".to_string()).or_insert(60);
|
||||||
|
println!("{}", x3);
|
||||||
|
let x2 = scores.entry("Red".to_string()).or_insert(55);
|
||||||
|
println!("{}", x2);
|
||||||
|
println!("{:?}", scores);
|
||||||
|
|
||||||
|
let nms = vec![String::from("Blue"), String::from("Yellow")];
|
||||||
|
let scs = vec![10, 50];
|
||||||
|
let scores: HashMap<_, _> = nms.iter().zip(scs.iter()).collect();
|
||||||
|
println!("{:?}", scores);
|
||||||
|
|
||||||
|
let field_name = String::from("hello");
|
||||||
|
let field_value = 456;
|
||||||
|
let mut map1 = HashMap::new();
|
||||||
|
map1.insert(field_name, field_value);
|
||||||
|
// borrow of moved value: `field_name`
|
||||||
|
// println!("{} {} {:?}", field_name, field_value, map1);
|
||||||
|
println!("{} {:?}", field_value, map1);
|
||||||
|
|
||||||
|
let k = &String::from("hello");
|
||||||
|
let v = String::from("world");
|
||||||
|
let mut map1 = HashMap::new();
|
||||||
|
map1.insert(k, v);
|
||||||
|
// borrow of moved value: `v`
|
||||||
|
// println!("{} {} {:?}", k, v, map1);
|
||||||
|
println!("{} {:?}", *k, map1);
|
||||||
|
|
||||||
|
let option = map1.get(k);
|
||||||
|
println!("{}", option.unwrap());
|
||||||
|
|
||||||
|
let text = "hello world wonderful world";
|
||||||
|
let mut map = HashMap::new();
|
||||||
|
for tex in text.split_whitespace() {
|
||||||
|
let counts = map.entry(tex).or_insert(0);
|
||||||
|
*counts += 1;
|
||||||
|
}
|
||||||
|
println!("{:?}", map);
|
||||||
|
|
||||||
|
println!("----------------错误处理-----------------");
|
||||||
|
let fnames = "hello2.txt";
|
||||||
|
let result = File::open(fnames);
|
||||||
|
let file = match result {
|
||||||
|
Ok(f) => f,
|
||||||
|
// Err(e) => {
|
||||||
|
// println!("Problem opening the file: {:?}", e);
|
||||||
|
//
|
||||||
|
// 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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
oe => panic!("Problem opening the file: {:?}", oe),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
println!("{:?}", file);
|
||||||
|
|
||||||
|
println!("----------------match 匹配 枚举------------------");
|
||||||
|
let m = Message::Quit;
|
||||||
|
match m {
|
||||||
|
Message::Write(s) => println!("{}", s),
|
||||||
|
abc => println!("{:?}", abc),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_tuple(t: (i32, &str)) {
|
fn test_tuple(t: (i32, &str)) {
|
||||||
@ -1268,7 +1347,7 @@ impl ToString for Tweet {
|
|||||||
//}
|
//}
|
||||||
|
|
||||||
fn notify<T, U>(t: &T, u: &U) -> String
|
fn notify<T, U>(t: &T, u: &U) -> String
|
||||||
where
|
where
|
||||||
T: Summary + ToString,
|
T: Summary + ToString,
|
||||||
U: Summary,
|
U: Summary,
|
||||||
{
|
{
|
||||||
@ -1338,7 +1417,7 @@ fn longest<'a>(x: &'a str, y: &'a str) -> &'a str {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct Cacher<T, D, F>
|
struct Cacher<T, D, F>
|
||||||
where
|
where
|
||||||
T: Fn(&D) -> F,
|
T: Fn(&D) -> F,
|
||||||
{
|
{
|
||||||
calculation: T,
|
calculation: T,
|
||||||
@ -1346,7 +1425,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T, D, F> Cacher<T, D, F>
|
impl<T, D, F> Cacher<T, D, F>
|
||||||
where
|
where
|
||||||
D: Hash + Eq,
|
D: Hash + Eq,
|
||||||
F: Copy,
|
F: Copy,
|
||||||
T: Fn(&D) -> F,
|
T: Fn(&D) -> F,
|
||||||
|
Loading…
Reference in New Issue
Block a user