update readme

This commit is contained in:
sunface
2022-02-23 17:11:31 +08:00
parent dc6a4fb48a
commit d4a716d36c
176 changed files with 407 additions and 1 deletions

79
en/src/SUMMARY.md Normal file
View File

@ -0,0 +1,79 @@
# Summary
- [Why Exercise.rs](why-exercise.md)
- [Variables todo](variables.md)
- [Basic Types todo](basic-types/intro.md)
- [Numbers](basic-types/numbers.md)
- [Char, Bool and Unit](basic-types/char-bool-unit.md)
- [Statements and Expressions](basic-types/statements-expressions.md)
- [Functions](basic-types/functions.md)
- [Ownership and Borrowing todo](ownership/intro.md)
- [Ownership](ownership/ownership.md)
- [Reference and Borrowing](ownership/borrowing.md)
- [Compound Types todo](compound-types/intro.md)
- [string and slice](compound-types/string-slice.md)
- [tuple](compound-types/tuple.md)
- [struct](compound-types/struct.md)
- [enum](compound-types/enum.md)
- [array](compound-types/array.md)
- [Flow Control todo](flow-control.md)
- [Pattern Match todo](pattern-match/intro.md)
- [match, if let](pattern-match/match-iflet.md)
- [Option destructing](pattern-match/option.md)
- [Patterns](pattern-match/patterns.md)
- [Method todo](method.md)
- [Generics and Traits todo](generics-traits/intro.md)
- [Generics](generics-traits/generics.md)
- [Traits](generics-traits/traits.md)
- [Trait Object](generics-traits/trait-object.md)
- [Advance Traits](generics-traits/advance-traits.md)
- [Collection Types todo](collections/intro.md)
- [Vector](collections/vector.md)
- [HashMap](collections/hashmap.md)
- [Type Conversion todo](type-conversion.md)
- [Result and panic todo](result-panic/intro.md)
- [panic!](result-panic/panic.md)
- [result and ?](result-panic/result.md)
- [Crate and module todo](crate-module/intro.md)
- [Crate](crate-module/crate.md)
- [Module](crate-module/module.md)
- [use and pub](crate-module/use-pub.md)
- [Comments and Docs todo](comments-docs.md)
- [Formatted output todo](formatted-output.md)
- [Lifetime todo](lifetime/intro.md)
- [basic](lifetime/basic.md)
- [&'static and T: 'static](lifetime/static.md)
- [advance](lifetime/advance.md)
- [Functional programing todo](functional-programing/intro.md)
- [Closure](functional-programing/cloure.md)
- [Iterator](functional-programing/iterator.md)
- [newtype and Sized todo](newtype-sized.md)
- [Smart pointers todo](smart-pointers/intro.md)
- [Box](smart-pointers/box.md)
- [Deref](smart-pointers/deref.md)
- [Drop](smart-pointers/drop.md)
- [Rc and Arc](smart-pointers/rc-arc.md)
- [Cell and RefCell](smart-pointers/cell-refcell.md)
- [Weak and Circle reference todo](weak.md)
- [Self referential todo](self-referential.md)
- [Threads todo](threads/intro.md)
- [Basic using](threads/basic-using.md)
- [Message passing](threads/message-passing.md)
- [Sync](threads/sync.md)
- [Atomic](threads/atomic.md)
- [Send and Sync](threads/send-sync.md)
- [Global variables todo](global-variables.md)
- [Errors todo](errors.md)
- [unsafe todo](unsafe.md)
- [macro todo](macro.md)
- [Tests todo](tests/intro.md)
- [Write Tests](tests/write-tests.md)
- [Benchmark](tests/benchmark.md)
- [Unit and Integration](tests/unit-integration.md)
- [Assertions](tests/assertions.md)
- [Async/Await todo](async/intro.md)
- [async and await!](async/async-await.md)
- [Future](async/future.md)
- [Pin and Unpin](async/pin-unpin.md)
- [Stream](async/stream.md)

View File

@ -0,0 +1 @@
# async and await!

1
en/src/async/future.md Normal file
View File

@ -0,0 +1 @@
# Future

1
en/src/async/intro.md Normal file
View File

@ -0,0 +1 @@
# Async/Await

View File

@ -0,0 +1 @@
# Pin and Unpin

1
en/src/async/stream.md Normal file
View File

@ -0,0 +1 @@
# Stream

View File

@ -0,0 +1 @@
# Char, Bool and Unit

View File

@ -0,0 +1 @@
# Functions

View File

@ -0,0 +1 @@
# Basic Types

View File

@ -0,0 +1 @@
# Numbers

View File

@ -0,0 +1 @@
# Statements and Expressions

1
en/src/circle-refer Normal file
View File

@ -0,0 +1 @@
# Weak and Circle reference

View File

@ -0,0 +1 @@
# Circle reference and Self referential

View File

@ -0,0 +1 @@
# HashMap

View File

@ -0,0 +1 @@
# Collection Types

View File

@ -0,0 +1 @@
# Vector

1
en/src/comments-docs.md Normal file
View File

@ -0,0 +1 @@
# Comments and Docs

View File

@ -0,0 +1 @@
# array

View File

@ -0,0 +1 @@
# enum

View File

@ -0,0 +1 @@
# Compound Types

View File

@ -0,0 +1 @@
# string and slice

View File

@ -0,0 +1 @@
# struct

View File

@ -0,0 +1 @@
# tuple

1
en/src/crate Normal file
View File

@ -0,0 +1 @@
# Crate

View File

@ -0,0 +1 @@
# Crate

View File

@ -0,0 +1 @@
# Crate and module

View File

@ -0,0 +1 @@
# Module

View File

@ -0,0 +1 @@
# use and pub

1
en/src/errors.md Normal file
View File

@ -0,0 +1 @@
# Errors

1
en/src/flow-control.md Normal file
View File

@ -0,0 +1 @@
# Flow Control

View File

@ -0,0 +1 @@
# Formatted output

View File

@ -0,0 +1,51 @@
# Closure
下面代码是Rust圣经课程中[闭包](http://course.rs/advance/functional-programing/closure.html#结构体中的闭包)章节的课内练习题答案:
```rust
struct Cacher<T,E>
where
T: Fn(E) -> E,
E: Copy
{
query: T,
value: Option<E>,
}
impl<T,E> Cacher<T,E>
where
T: Fn(E) -> E,
E: Copy
{
fn new(query: T) -> Cacher<T,E> {
Cacher {
query,
value: None,
}
}
fn value(&mut self, arg: E) -> E {
match self.value {
Some(v) => v,
None => {
let v = (self.query)(arg);
self.value = Some(v);
v
}
}
}
}
fn main() {
}
#[test]
fn call_with_different_values() {
let mut c = Cacher::new(|a| a);
let v1 = c.value(1);
let v2 = c.value(2);
assert_eq!(v2, 1);
}
```

View File

@ -0,0 +1 @@
# Functional programing

View File

@ -0,0 +1 @@
# Iterator

View File

@ -0,0 +1,51 @@
# Closure
下面代码是Rust圣经课程中[闭包](http://course.rs/advance/functional-programing/closure.html#结构体中的闭包)章节的课内练习题答案:
```rust
struct Cacher<T,E>
where
T: Fn(E) -> E,
E: Copy
{
query: T,
value: Option<E>,
}
impl<T,E> Cacher<T,E>
where
T: Fn(E) -> E,
E: Copy
{
fn new(query: T) -> Cacher<T,E> {
Cacher {
query,
value: None,
}
}
fn value(&mut self, arg: E) -> E {
match self.value {
Some(v) => v,
None => {
let v = (self.query)(arg);
self.value = Some(v);
v
}
}
}
}
fn main() {
}
#[test]
fn call_with_different_values() {
let mut c = Cacher::new(|a| a);
let v1 = c.value(1);
let v2 = c.value(2);
assert_eq!(v2, 1);
}
```

View File

@ -0,0 +1 @@
# Functional Programming

View File

@ -0,0 +1 @@
# Advance Traits

View File

@ -0,0 +1 @@
# Generics

View File

@ -0,0 +1 @@
# Generics and Traits

View File

@ -0,0 +1 @@
# Trait Object

View File

@ -0,0 +1 @@
# Traits

View File

@ -0,0 +1 @@
# Global variables

View File

@ -0,0 +1 @@
# advance

26
en/src/lifetime/basic.md Normal file
View File

@ -0,0 +1,26 @@
## 生命周期消除
```rust
fn print(s: &str); // elided
fn print<'a>(s: &'a str); // expanded
fn debug(lvl: usize, s: &str); // elided
fn debug<'a>(lvl: usize, s: &'a str); // expanded
fn substr(s: &str, until: usize) -> &str; // elided
fn substr<'a>(s: &'a str, until: usize) -> &'a str; // expanded
fn get_str() -> &str; // ILLEGAL
fn frob(s: &str, t: &str) -> &str; // ILLEGAL
fn get_mut(&mut self) -> &mut T; // elided
fn get_mut<'a>(&'a mut self) -> &'a mut T; // expanded
fn args<T: ToCStr>(&mut self, args: &[T]) -> &mut Command // elided
fn args<'a, 'b, T: ToCStr>(&'a mut self, args: &'b [T]) -> &'a mut Command // expanded
fn new(buf: &mut [u8]) -> BufWriter; // elided
fn new(buf: &mut [u8]) -> BufWriter<'_>; // elided (with `rust_2018_idioms`)
fn new<'a>(buf: &'a mut [u8]) -> BufWriter<'a> // expanded
```

1
en/src/lifetime/intro.md Normal file
View File

@ -0,0 +1 @@
# Lifetime

49
en/src/lifetime/static.md Normal file
View File

@ -0,0 +1,49 @@
# &'static and T: 'static
```rust,editable
use std::fmt::Display;
fn main() {
let mut string = "First".to_owned();
string.push_str(string.to_uppercase().as_str());
print_a(&string);
print_b(&string);
print_c(&string); // Compilation error
print_d(&string); // Compilation error
print_e(&string);
print_f(&string);
print_g(&string); // Compilation error
}
fn print_a<T: Display + 'static>(t: &T) {
println!("{}", t);
}
fn print_b<T>(t: &T)
where
T: Display + 'static,
{
println!("{}", t);
}
fn print_c(t: &'static dyn Display) {
println!("{}", t)
}
fn print_d(t: &'static impl Display) {
println!("{}", t)
}
fn print_e(t: &(dyn Display + 'static)) {
println!("{}", t)
}
fn print_f(t: &(impl Display + 'static)) {
println!("{}", t)
}
fn print_g(t: &'static String) {
println!("{}", t);
}
```

1
en/src/macro.md Normal file
View File

@ -0,0 +1 @@
# macro

1
en/src/method.md Normal file
View File

@ -0,0 +1 @@
# Method

1
en/src/newtype-sized.md Normal file
View File

@ -0,0 +1 @@
# newtype and Sized

View File

@ -0,0 +1 @@
# Reference and Borrowing

View File

@ -0,0 +1 @@
# Ownership and Borrowing

View File

@ -0,0 +1 @@
# Ownership

View File

@ -0,0 +1 @@
# Pattern Match

View File

@ -0,0 +1 @@
# match, if let

View File

@ -0,0 +1 @@
# Option destructing

View File

@ -0,0 +1 @@
# Patterns

1
en/src/resu Normal file
View File

@ -0,0 +1 @@
# panic!

View File

@ -0,0 +1 @@
# Result and panic

View File

@ -0,0 +1 @@
# panic!

View File

@ -0,0 +1 @@
# result and ?

View File

@ -0,0 +1 @@
# Self referential

View File

@ -0,0 +1 @@
# Box

View File

@ -0,0 +1 @@
# Cell and RefCell

View File

@ -0,0 +1 @@
# Deref

View File

@ -0,0 +1 @@
# Drop

View File

@ -0,0 +1 @@
# Smart pointers

View File

@ -0,0 +1 @@
# Rc and Arc

View File

@ -0,0 +1 @@
# Assertions

View File

@ -0,0 +1,3 @@
# Benchmark
https://doc.rust-lang.org/unstable-book/library-features/test.html

1
en/src/tests/intro.md Normal file
View File

@ -0,0 +1 @@
# Tests

View File

@ -0,0 +1 @@
# Unit and Integration

View File

@ -0,0 +1 @@
# Write Tests

1
en/src/threads/atomic.md Normal file
View File

@ -0,0 +1 @@
# Atomic

View File

@ -0,0 +1 @@
# Basic using

1
en/src/threads/intro.md Normal file
View File

@ -0,0 +1 @@
# Threads

View File

@ -0,0 +1 @@
# Message passing

View File

@ -0,0 +1 @@
# Send and Sync

1
en/src/threads/sync.md Normal file
View File

@ -0,0 +1 @@
# Sync

View File

@ -0,0 +1 @@
# Type Conversion

1
en/src/unsafe.md Normal file
View File

@ -0,0 +1 @@
# unsafe

1
en/src/variables.md Normal file
View File

@ -0,0 +1 @@
# Variables

1
en/src/weak.md Normal file
View File

@ -0,0 +1 @@
# Weak and Circle reference

1
en/src/why-exercise.md Normal file
View File

@ -0,0 +1 @@
# Why Exercise.rs