-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathfn_pointer.rs
More file actions
34 lines (27 loc) · 757 Bytes
/
fn_pointer.rs
File metadata and controls
34 lines (27 loc) · 757 Bytes
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
#![allow(unused)]
// Function pointer
fn add(x: u32, y: u32) -> u32 {
x + y
}
fn do_twice(f: fn(u32, u32) -> u32, x: u32, y: u32) -> u32 {
f(x, y) + f(x, y)
}
fn push(v: &mut Vec<u32>, x: u32) {
v.push(x);
}
fn fn_pointer_mut(f: fn(&mut Vec<u32>, u32), v: &mut Vec<u32>, x: u32) {
f(v, x);
f(v, x);
}
fn main() {
// Store a function pointer to a variable
let f: fn(u32, u32) -> u32 = add;
println!("f: {}", f(1, 2));
// Function pointer as input to a function
let z = do_twice(add, 1, 2);
println!("fn pointer: {z}");
// Function pointer that mutates a data as input to a function
let mut v: Vec<u32> = vec![1, 2, 3];
fn_pointer_mut(push, &mut v, 4);
println!("fn pointer mut: {:?}", v);
}