From 562eb9247ff04f993e724b882b9777bf805e7524 Mon Sep 17 00:00:00 2001 From: sunface Date: Thu, 31 Mar 2022 13:49:17 +0800 Subject: [PATCH] Add [Closure] chapter --- en/src/SUMMARY.md | 4 +- en/src/functional-programing/cloure.md | 40 +++++++++++++++-- solutions/functional-programing/closure.md | 51 ++++++++++++++++++++++ zh-CN/src/basic-types/char-bool-unit.md | 2 +- 4 files changed, 91 insertions(+), 6 deletions(-) diff --git a/en/src/SUMMARY.md b/en/src/SUMMARY.md index 1ddb95f..d0facbc 100644 --- a/en/src/SUMMARY.md +++ b/en/src/SUMMARY.md @@ -53,9 +53,9 @@ - [basic](lifetime/basic.md) - [&'static and T: 'static](lifetime/static.md) - [advanced](lifetime/advance.md) -- [Functional programing TODO](functional-programing/intro.md) +- [Functional programing](functional-programing/intro.md) - [Closure](functional-programing/cloure.md) - - [Iterator](functional-programing/iterator.md) + - [Iterator TODO](functional-programing/iterator.md) - [newtype and Sized TODO](newtype-sized.md) - [Smart pointers TODO](smart-pointers/intro.md) - [Box](smart-pointers/box.md) diff --git a/en/src/functional-programing/cloure.md b/en/src/functional-programing/cloure.md index 7427fcf..8df7250 100644 --- a/en/src/functional-programing/cloure.md +++ b/en/src/functional-programing/cloure.md @@ -328,10 +328,44 @@ fn main() { ## Closure as return types Returning a closure is much harder than you may thought of. +10γ€πŸŒŸπŸŒŸ +```rust,editable +/* Fill in the blank using two approches, + and fix the errror */ +fn create_fn() -> __ { + let num = 5; - \ No newline at end of file +``` \ No newline at end of file diff --git a/solutions/functional-programing/closure.md b/solutions/functional-programing/closure.md index 9e56894..3bab855 100644 --- a/solutions/functional-programing/closure.md +++ b/solutions/functional-programing/closure.md @@ -231,4 +231,55 @@ fn main() { call_me(closure); call_me(function); } +``` + +10、 +```rust +/* Fill in the blank and fix the errror */ +// You can aslo use `impl FnOnce(i32) -> i32` +fn create_fn() -> impl Fn(i32) -> i32 { + let num = 5; + + move |x| x + num +} + + +fn main() { + let fn_plain = create_fn(); + fn_plain(1); +} +``` + +```rust +/* Fill in the blank and fix the errror */ +fn create_fn() -> Box i32> { + let num = 5; + + // how does the following closure capture the evironment variable `num` + // &T, &mut T, T ? + Box::new(move |x| x + num) +} + + +fn main() { + let fn_plain = create_fn(); + fn_plain(1); +} +``` + +11、 +```rust +// Every closure has its own type. Even if one closure has the same representation as another, their types are different. +fn factory(x:i32) -> Box i32> { + + let num = 5; + + if x > 1{ + Box::new(move |x| x + num) + } else { + Box::new(move |x| x + num) + } +} + +fn main() {} ``` \ No newline at end of file diff --git a/zh-CN/src/basic-types/char-bool-unit.md b/zh-CN/src/basic-types/char-bool-unit.md index 8b90ff9..a859d26 100644 --- a/zh-CN/src/basic-types/char-bool-unit.md +++ b/zh-CN/src/basic-types/char-bool-unit.md @@ -66,7 +66,7 @@ fn main() { let _v: () = (); let v = (2, 3); - assert_eq!(v, implicitly_ret_unit()) + assert_eq!(v, implicitly_ret_unit()); println!("Success!") }