Function pointers in Rust

 Reprinted: https://www.cnblogs.com/liujin-now/p/17342328.html

What is a function pointer

Passing a function pointer allows us to use a function as an argument to another function. Functions are of type fn (use lowercase "f") to avoid confusion with the Fn closure trait. fn is called a function pointer. The syntax for specifying a parameter as a function pointer is similar to a closure.

Function pointer types (  fn written using keywords) point to functions for which the function identifier does not have to be known at compile time. They can also be created from function item types or non-capturing closures with an automatic coercion

How to define and use function pointers in Rust

Here is a simple code example that demonstrates how to define and use function pointers in Rust:

fn add_one(x: i32) -> i32 {
x + 1 
} 
fn do_twice(f: fn(i32) -> i32, arg: i32) -> i32 {
f(arg) + f(arg) 
} 
fn main() { 
let answer = do_twice(add_one, 5);
println!("The answer is: {}", answer); 
}
复制代码

This prints The answer is: 12. f in do_twice is specified as a fn that takes an i32 argument and returns an i32. Then f can be called in the body of the do_twice function. In main, you can pass the function name add_one as the first argument to do_twice.

The difference between function pointers and closures

Both function pointers and closures can be used to represent callable objects, but there are some important differences between them. One of the differences is that a closure can capture variables in its surrounding environment, whereas a function pointer cannot.

Unlike closures, fn is a type rather than a trait, so specifying fn directly as a parameter instead of declaring a generic parameter with Fn as a trait bound.

Function pointers implement all three closure traits (Fn, FnMut, and FnOnce), so it is always possible to pass a function pointer as an argument when calling a function that expects a closure.

Prefer writing functions that use the generics and closure trait so that it accepts functions or closures as arguments. An example of a situation where it is expected to accept only fn and not closures is when interacting with external code where no closures exist: functions in C can accept functions as arguments, but C does not have closures.

Application scenarios of function pointers

  • Can be passed as a parameter to other functions to be called inside the function. This is very common in some higher-order functions, such as  map sum and  filter so on.

  • Function pointers can also be used to define callback functions (callback functions), for example in event-driven programming (event-driven programming).

  • Function pointers can also be stored in data structures to be called later. This is very useful in some algorithms, such as sorting algorithms.

Advantages and disadvantages of function pointers

One of the advantages of function pointers is that they have no runtime overhead. This means they can be used to represent callable objects without affecting performance.

However, function pointers also have some limitations. For example, they cannot capture variables in their surrounding environment, which makes them less flexible than closures. Also, function pointers can only point to functions known at compile time, which means they cannot be used to denote anonymous functions. from Liu Jin, please indicate the original text link when reprinting. grateful!

Guess you like

Origin blog.csdn.net/jingdianjiuchan/article/details/130481735