@@ -9,12 +9,16 @@ implement special [`Fn`](https://doc.rust-lang.org/std/ops/trait.Fn.html),
9
9
[ ` FnMut ` ] ( https://doc.rust-lang.org/std/ops/trait.FnMut.html ) , and
10
10
[ ` FnOnce ` ] ( https://doc.rust-lang.org/std/ops/trait.FnOnce.html ) traits:
11
11
12
- The special type ` fn ` refers to function pointers - either the address of a
13
- function, or a closure that captures nothing.
12
+ The special types ` fn(..) -> T ` refer to function pointers - either the address
13
+ of a function, or a closure that captures nothing.
14
14
15
15
``` rust,editable
16
- fn apply_and_log(func: impl FnOnce(String) -> String, func_name: &str, input: &str) {
17
- println!("Calling {func_name}({input}): {}", func(input.to_string()))
16
+ fn apply_and_log(
17
+ func: impl FnOnce(&'static str) -> String,
18
+ func_name: &'static str,
19
+ input: &'static str,
20
+ ) {
21
+ println!("Calling {func_name}({input}): {}", func(input))
18
22
}
19
23
20
24
fn main() {
@@ -32,9 +36,10 @@ fn main() {
32
36
apply_and_log(&mut accumulate, "accumulate", "green");
33
37
apply_and_log(&mut accumulate, "accumulate", "blue");
34
38
35
- let take_and_reverse = |mut prefix: String| {
36
- prefix.push_str(&v.into_iter().rev().collect::<Vec<_>>().join("/"));
37
- prefix
39
+ let take_and_reverse = |prefix| {
40
+ let mut acc = String::from(prefix);
41
+ acc.push_str(&v.into_iter().rev().collect::<Vec<_>>().join("/"));
42
+ acc
38
43
};
39
44
apply_and_log(take_and_reverse, "take_and_reverse", "reversed: ");
40
45
}
0 commit comments