From eb26814579bc4d83fe53967d0c5437212f7d6e08 Mon Sep 17 00:00:00 2001 From: Dhghomon <56599343+Dhghomon@users.noreply.github.com> Date: Thu, 18 Mar 2021 12:41:46 +0900 Subject: [PATCH] Update README.md --- README.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 03049ce..b4f8290 100644 --- a/README.md +++ b/README.md @@ -8735,19 +8735,19 @@ fn map(self, f: F) -> Map // 🚧 `fn map(self, f: F)` mean that it takes two generic types. `F` is a function that takes one item from the container implementing `.map()` and `B` is the return type of that function. Then after the `where` we see the trait bounds. ("Trait bound" means "it must have this trait".) One is `Sized`, and the next is the closure signature. It must be an `FnMut`, and do the closure on `Self::Item`, which is the iterator that you give it. Then it returns `B`. -So we can do the same thing to return a closure. To return a closure, use `impl` and then the closure signature. Once you return it, you can use it just like a function. Here is a small example of a function that gives you a closure depending on the number you put in. If you put 2 or 40 in then it multiplies it, and otherwise it gives you the same number. Because it's a closure we can do anything we want, so we also print a message. +So we can do the same thing to return a closure. To return a closure, use `impl` and then the closure signature. Once you return it, you can use it just like a function. Here is a small example of a function that gives you a closure depending on the text you put in. If you put "double" or "triple" in then it multiplies it by 2 or 3, and otherwise it gives you the same number. Because it's a closure we can do anything we want, so we also print a message. ```rust -fn returns_a_closure(input: u8) -> impl FnMut(i32) -> i32 { +fn returns_a_closure(input: &str) -> impl FnMut(i32) -> i32 { match input { - 2 => |mut number| { + "double" => |mut number| { number *= 2; - println!("Your number is {}", number); + println!("Doubling number. Now it is {}", number); number }, - 40 => |mut number| { + "triple" => |mut number| { number *= 40; - println!("Your number is {}", number); + println!("Tripling number. Now it is {}", number); number }, _ => |number| { @@ -8761,13 +8761,13 @@ fn main() { let my_number = 10; // Make three closures - let mut give_two = returns_a_closure(2); - let mut give_forty = returns_a_closure(40); - let mut give_fifty = returns_a_closure(50); + let mut doubles = returns_a_closure("double"); + let mut triples = returns_a_closure("triple"); + let mut quadruples = returns_a_closure("quadruple"); - give_two(my_number); - give_forty(my_number); - give_fifty(my_number); + doubles(my_number); + triples(my_number); + quadruples(my_number); } ```