-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclosures.rs
More file actions
53 lines (41 loc) · 1.14 KB
/
Copy pathclosures.rs
File metadata and controls
53 lines (41 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
fn main() {
let outer_var = String::from("23");
fn function(input: i32) -> i32 {
input
// + outer_var
}
fn call_closure<F>(f: F, arg: i32)
where
F: FnOnce(i32) -> i32,
{
f(arg);
}
let test = |x: i32| {
std::mem::drop(outer_var);
return x + 1;
};
call_closure(test, 1);
fn a_function_that_returns_closure() -> impl FnOnce() {
let mut x = "hy".to_owned();
let clo = move || {
x += "fy";
println!("x is {x}");
std::mem::drop(x);
};
clo
}
let xx = a_function_that_returns_closure();
xx();
let vec1 = vec![
String::from("A"),
String::from("B"),
String::from("C"),
String::from("D"),
];
let is_present = vec1.iter().any(|ele| *ele == String::from("C"));
println!("is_present : {is_present}");
let is_find = vec1.iter().find(|ele| **ele == String::from("C"));
println!("is_find : {:?}", is_find);
let position_of_b = vec1.iter().position(|ele| *ele == String::from("B"));
println!("position_of_b : {:?}", position_of_b);
}