diff --git a/refs.md b/refs.md new file mode 100644 index 0000000..2831c5d --- /dev/null +++ b/refs.md @@ -0,0 +1,4 @@ +1. https://github.com/vinodotdev/node-to-rust +2. https://doc.rust-lang.org/stable/rust-by-example/primitives.html +3. https://github.com/dtolnay/rust-quiz +4. \ No newline at end of file diff --git a/src/SUMMARY.md b/src/SUMMARY.md index 67c21db..602bdc8 100644 --- a/src/SUMMARY.md +++ b/src/SUMMARY.md @@ -1,4 +1,11 @@ # Summary - [Lifetime](lifetime/intro.md) - - [&'static and T: 'static](lifetime/static.md) \ No newline at end of file + - [basic](lifetime/basic.md) + - [&'static and T: 'static](lifetime/static.md) + +- [Tests](tests/intro.md) + - [Benchmark](tests/benchmark.md) + +- [Functional Programming](functional-programming/intro.md) + - [Closure](functional-programming/closure.md) \ No newline at end of file diff --git a/src/functional-programming/closure.md b/src/functional-programming/closure.md new file mode 100644 index 0000000..fb73740 --- /dev/null +++ b/src/functional-programming/closure.md @@ -0,0 +1,51 @@ +# Closure + +下面代码是Rust圣经课程中[闭包](http://course.rs/advance/functional-programing/closure.html#结构体中的闭包)章节的课内练习题答案: + +```rust +struct Cacher +where + T: Fn(E) -> E, + E: Copy +{ + query: T, + value: Option, +} + +impl Cacher +where + T: Fn(E) -> E, + E: Copy +{ + fn new(query: T) -> Cacher { + 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); +} +``` \ No newline at end of file diff --git a/src/functional-programming/intro.md b/src/functional-programming/intro.md new file mode 100644 index 0000000..dca041e --- /dev/null +++ b/src/functional-programming/intro.md @@ -0,0 +1 @@ +# Functional Programming diff --git a/src/lifetime/basic.md b/src/lifetime/basic.md new file mode 100644 index 0000000..40439f4 --- /dev/null +++ b/src/lifetime/basic.md @@ -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(&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 +``` \ No newline at end of file diff --git a/src/tests/benchmark.md b/src/tests/benchmark.md new file mode 100644 index 0000000..f883201 --- /dev/null +++ b/src/tests/benchmark.md @@ -0,0 +1,3 @@ +# Benchmark + +https://doc.rust-lang.org/unstable-book/library-features/test.html \ No newline at end of file diff --git a/src/tests/intro.md b/src/tests/intro.md new file mode 100644 index 0000000..007eb95 --- /dev/null +++ b/src/tests/intro.md @@ -0,0 +1 @@ +# Tests