add toc
This commit is contained in:
parent
0e2a8810b5
commit
f252acdee4
|
@ -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.
|
|
@ -1,4 +1,11 @@
|
|||
# Summary
|
||||
|
||||
- [Lifetime](lifetime/intro.md)
|
||||
- [&'static and T: 'static](lifetime/static.md)
|
||||
- [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)
|
|
@ -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);
|
||||
}
|
||||
```
|
|
@ -0,0 +1 @@
|
|||
# Functional Programming
|
|
@ -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
|
||||
```
|
|
@ -0,0 +1,3 @@
|
|||
# Benchmark
|
||||
|
||||
https://doc.rust-lang.org/unstable-book/library-features/test.html
|
|
@ -0,0 +1 @@
|
|||
# Tests
|
Loading…
Reference in New Issue