update at 2020-06-16 18:18:40 by ehlxr
parent
4c9a6dec3c
commit
939f409503
63
src/main.rs
63
src/main.rs
|
@ -331,15 +331,15 @@ fn main() {
|
||||||
let v: Vec<_> = counter.into_iter().map(|x| x + 1).collect();
|
let v: Vec<_> = counter.into_iter().map(|x| x + 1).collect();
|
||||||
println!("{:?}", v);
|
println!("{:?}", v);
|
||||||
let v: Vec<_> = Counter::new()
|
let v: Vec<_> = Counter::new()
|
||||||
.zip(Counter::new().skip(1))
|
.zip(Counter::new().skip(1))
|
||||||
.map(|(a, b)| a * b)
|
.map(|(a, b)| a * b)
|
||||||
.collect();
|
.collect();
|
||||||
println!("{:?}", v);
|
println!("{:?}", v);
|
||||||
let sum: u32 = Counter::new()
|
let sum: u32 = Counter::new()
|
||||||
.zip(Counter::new().skip(1))
|
.zip(Counter::new().skip(1))
|
||||||
.map(|(a, b)| a * b)
|
.map(|(a, b)| a * b)
|
||||||
.filter(|x| x % 3 == 0)
|
.filter(|x| x % 3 == 0)
|
||||||
.sum();
|
.sum();
|
||||||
println!("{}", sum);
|
println!("{}", sum);
|
||||||
|
|
||||||
// ---------------------------
|
// ---------------------------
|
||||||
|
@ -409,7 +409,7 @@ fn main() {
|
||||||
leaf.parent.borrow().upgrade(),
|
leaf.parent.borrow().upgrade(),
|
||||||
);
|
);
|
||||||
} // -> 离开作用域,此时,Rc<branch> 的强引用(strong_count)为 0,但是 Weak<branch> 弱引用数(weak_count)仍然为 1(引用 Weak<branch> 的 Rc<leaf> 仍然存在),
|
} // -> 离开作用域,此时,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!(
|
println!(
|
||||||
"leaf strong = {}, weak = {}, parent = {:?}",
|
"leaf strong = {}, weak = {}, parent = {:?}",
|
||||||
Rc::strong_count(&leaf),
|
Rc::strong_count(&leaf),
|
||||||
|
@ -937,7 +937,7 @@ fn main() {
|
||||||
println!("{}", s);
|
println!("{}", s);
|
||||||
let mut s: String = "Hello".to_string();
|
let mut s: String = "Hello".to_string();
|
||||||
let w = "World";
|
let w = "World";
|
||||||
s.push_str(w);// 并不需要获取 w 的所有权
|
s.push_str(w); // 并不需要获取 w 的所有权
|
||||||
s.push('!');
|
s.push('!');
|
||||||
println!("{} {}", s, w);
|
println!("{} {}", s, w);
|
||||||
|
|
||||||
|
@ -948,18 +948,21 @@ fn main() {
|
||||||
let s1 = String::from("Hello, ");
|
let s1 = String::from("Hello, ");
|
||||||
let s2 = String::from("World!");
|
let s2 = String::from("World!");
|
||||||
let s3 = s1 + &s2; // 注意 s1 被移动了,不能继续使用
|
let s3 = s1 + &s2; // 注意 s1 被移动了,不能继续使用
|
||||||
// println!("{} {} {}", s1, s3, s2);
|
// println!("{} {} {}", s1, s3, s2);
|
||||||
println!("{} {}", s3, s2);
|
println!("{} {}", s3, s2);
|
||||||
|
|
||||||
for c in "नमˑे".chars() {
|
let hello = "测试中文字符串";
|
||||||
println!("{}", c);
|
for c in hello.chars() {
|
||||||
|
print!("{} ", c);
|
||||||
}
|
}
|
||||||
for b in "नमˑे".bytes() {
|
println!("");
|
||||||
println!("{}", b);
|
for b in hello.bytes() {
|
||||||
|
print!("{} ", b);
|
||||||
}
|
}
|
||||||
let hello = "Здравствуйте";
|
println!("");
|
||||||
let s = &hello[0..4];
|
// let s = hello[1]; // Rust 的字符串不支持索引
|
||||||
println!("&hello[0..4]={}", s);
|
// 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]);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_tuple(t: (i32, &str)) {
|
fn test_tuple(t: (i32, &str)) {
|
||||||
|
@ -1265,9 +1268,9 @@ 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,
|
||||||
{
|
{
|
||||||
format!("{}, {}", u.summarize(), t.to_string())
|
format!("{}, {}", u.summarize(), t.to_string())
|
||||||
}
|
}
|
||||||
|
@ -1335,18 +1338,18 @@ 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,
|
||||||
value: HashMap<D, F>,
|
value: HashMap<D, F>,
|
||||||
}
|
}
|
||||||
|
|
||||||
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,
|
||||||
{
|
{
|
||||||
fn new(calculation: T) -> Cacher<T, D, F> {
|
fn new(calculation: T) -> Cacher<T, D, F> {
|
||||||
Self {
|
Self {
|
||||||
|
@ -1406,10 +1409,10 @@ struct Shoe {
|
||||||
|
|
||||||
fn shoes_in_my_size(shoes: Vec<Shoe>, shoe_size: u32) -> Vec<Shoe> {
|
fn shoes_in_my_size(shoes: Vec<Shoe>, shoe_size: u32) -> Vec<Shoe> {
|
||||||
shoes
|
shoes
|
||||||
// .iter() // a collection of type `std::vec::Vec<Shoe>` cannot be built from `std::iter::Iterator<Item=&Shoe>`
|
// .iter() // a collection of type `std::vec::Vec<Shoe>` cannot be built from `std::iter::Iterator<Item=&Shoe>`
|
||||||
.into_iter() //获取 vector 所有权的迭代器
|
.into_iter() //获取 vector 所有权的迭代器
|
||||||
.filter(|s| s.size == shoe_size)
|
.filter(|s| s.size == shoe_size)
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Counter {
|
struct Counter {
|
||||||
|
|
Loading…
Reference in New Issue