std::ops::FnOnce - Rust (original) (raw)
Trait std::ops::FnOnce1.0.0 [−] [src]
#[lang = "fn_once"]
pub trait FnOnce { type Output; extern "rust-call" fn call_once(self, args: Args) -> Self::Output; }
The version of the call operator that takes a by-value receiver.
Instances of FnOnce
can be called, but might not be callable multiple times. Because of this, if the only thing known about a type is that it implements FnOnce
, it can only be called once.
FnOnce
is implemented automatically by closure that might consume captured variables, as well as all types that implement FnMut, e.g. (safe)function pointers (since FnOnce
is a supertrait of FnMut).
Since both Fn and FnMut are subtraits of FnOnce
, any instance ofFn or FnMut can be used where a FnOnce
is expected.
Use FnOnce
as a bound when you want to accept a parameter of function-like type and only need to call it once. If you need to call the parameter repeatedly, use FnMut as a bound; if you also need it to not mutate state, use Fn.
See the chapter on closures in The Rust Programming Language for some more information on this topic.
Also of note is the special syntax for Fn
traits (e.g.Fn(usize, bool) -> usize
). Those interested in the technical details of this can refer to the relevant section in the Rustonomicon.
let x = 5; let square_x = move || x * x; assert_eq!(square_x(), 25);Run
fn consume_with_relish(func: F) where F: FnOnce() -> String {
println!("Consumed: {}", func());
println!("Delicious!");
}
let x = String::from("x"); let consume_and_return_x = move || x; consume_with_relish(consume_and_return_x);
type [Output](#associatedtype.Output)
1.12.0
The returned type after the call operator is used.
extern "rust-call" fn [call_once](#tymethod.call%5Fonce)(self, args: Args) -> Self::[Output](../../std/ops/trait.FnOnce.html#associatedtype.Output "type std::ops::FnOnce::Output")
🔬 This is a nightly-only experimental API. (fn_traits
#29625)
Performs the call operation.
impl<'a, A, F> FnOnce<A> for [&'a ](../primitive.reference.html)F where F: [Fn](../../std/ops/trait.Fn.html "trait std::ops::Fn")<A> + ?[Sized](../../std/marker/trait.Sized.html "trait std:📑:Sized"), type [Output](#associatedtype.Output) = <F as [FnOnce](../../std/ops/trait.FnOnce.html "trait std::ops::FnOnce")<A>>::[Output](../../std/ops/trait.FnOnce.html#associatedtype.Output "type std::ops::FnOnce::Output");
impl<'a, A, F> FnOnce<A> for [&'a mut ](../primitive.reference.html)F where F: [FnMut](../../std/ops/trait.FnMut.html "trait std::ops::FnMut")<A> + ?[Sized](../../std/marker/trait.Sized.html "trait std:📑:Sized"), type [Output](#associatedtype.Output) = <F as [FnOnce](../../std/ops/trait.FnOnce.html "trait std::ops::FnOnce")<A>>::[Output](../../std/ops/trait.FnOnce.html#associatedtype.Output "type std::ops::FnOnce::Output");
impl<'a, A, R> FnOnce<A> for [Box](../../std/boxed/struct.Box.html "struct std::boxed::Box")<[FnBox](../../std/boxed/trait.FnBox.html "trait std::boxed::FnBox")<A, Output = R> + 'a> type [Output](#associatedtype.Output) = R;
impl<'a, A, R> FnOnce<A> for [Box](../../std/boxed/struct.Box.html "struct std::boxed::Box")<[FnBox](../../std/boxed/trait.FnBox.html "trait std::boxed::FnBox")<A, Output = R> + 'a + [Send](../../std/marker/trait.Send.html "trait std:📑:Send")> type [Output](#associatedtype.Output) = R;
impl<R, F: [FnOnce](../../std/ops/trait.FnOnce.html "trait std::ops::FnOnce")() -> R> FnOnce<[()](../primitive.unit.html)> for [AssertUnwindSafe](../../std/panic/struct.AssertUnwindSafe.html "struct std::panic::AssertUnwindSafe")<F> type [Output](#associatedtype.Output) = R;