Fn
The version of the call operator that takes a by-snapshot receiver.Instances of Fn
can be called repeatedly.Fn
is implemented automatically by closures which only whose captured variable are all Copy
. Additionally, for any type F
that implements Fn
, @F
implements Fn
, too.Since FnOnce
is implemented for all implementers of Fn
, any instance of Fn
can be used as a parameter where a FnOnce
is expected.Use Fn
as a bound when you want to accept a parameter of function-like type and need to call it repeatedly. If you do not need such strict requirements, use FnOnce
as bounds. # Examples ## Calling a closure
let square = |x| x * x;
assert_eq!(square(5), 25);
Using a Fn
parameter
fn call_with_one<F, +Drop<F>, +core::ops::Fn<F, (usize,)>[Output: usize]>(func: F) -> usize {
func(1)
}
let double = |x| x * 2;
assert_eq!(call_with_one(double), 2);
Fully qualified path: core::ops::function::Fn
pub trait Fn<T, Args>
Trait functions
call
Performs the call operation.
Fully qualified path: core::ops::function::Fn::call
fn call(self: @T, args: Args) -> Self::Output
Trait types
Output
The returned type after the call operator is used.
Fully qualified path: core::ops::function::Fn::Output
type Output;