study_example/advance_feature/
advanced_function_closures.rs1fn add_one(x: i32) -> i32 {
2 x + 1
3}
4
5fn do_twice(f: fn(i32) -> i32, arg: i32) -> i32 {
6 f(arg) + f(arg)
7}
8
9fn function_pointer() {
14 let answer = do_twice(add_one, 3);
15 println!("do_twice answer: {answer}");
16}
17
18fn iterator_map_usage_closures() {
23 let list_of_numbers = vec![1, 2, 3];
24 let list_of_strings: Vec<String> = list_of_numbers.iter().map(|i| i.to_string()).collect();
25 println!("list of strings: {:?}", list_of_strings);
26}
27
28fn iterator_map_usage_fn_pointer() {
33 let list_of_numbers = vec![1, 2, 3];
34 let list_of_strings: Vec<String> = list_of_numbers.iter().map(ToString::to_string).collect();
35 println!("list of strings: {:?}", list_of_strings);
36}
37
38fn init_enum_type() {
43 #[derive(Debug)]
44 enum Status {
45 Value(u32),
46 Stop,
47 }
48 let list_of_status: Vec<Status> = (0u32..5).map(Status::Value).collect();
49 println!("list of status: {:?}", list_of_status);
50}
51
52fn return_closure() -> Box<dyn Fn(i32) -> i32> {
63 Box::new(|x| x + 1)
64}
65
66pub fn advanced_function_closures_study() {
71 function_pointer();
72 iterator_map_usage_closures();
73 iterator_map_usage_fn_pointer();
74 init_enum_type();
75 let f = return_closure();
76 println!("return_closure i = 32: {}", f(32));
77}