Trait std::ops::Fn  1.0.0[−][src]
#[must_use = "closures are lazy and do nothing unless called"] pub trait Fn<Args>: FnMut<Args> { extern "rust-call" fn call(&self, args: Args) -> Self::Output; }
Expand description
采用不可变接收者的调用运算符的版本。
Fn 的实例可以在不改变状态的情况下重复调用。
请勿将此 trait (Fn) 与 function pointers (fn) 混淆。
Fn 由闭包自动实现,闭包只对捕获的变量进行不可变引用或根本不捕获任何内容,还有 (安全) function pointers (有一些警告,请参见其文档以获取更多详细信息)。
此外,对于实现 Fn 的任何类型 F,&F 也实现了 Fn。
由于 FnMut 和 FnOnce 都是 Fn 的 supertraits,因此 Fn 的任何实例都可以用作参数,其中需要 FnMut 或 FnOnce。
当您要接受类似函数类型的参数并且需要反复调用且不改变状态 (例如,同时调用它) 时,请使用 Fn 作为绑定。
如果不需要严格的要求,请使用 FnMut 或 FnOnce 作为界限。
有关此主题的更多信息,请参见 Rust 编程语言 中关于闭包的章节。
还要注意的是 Fn traits 的特殊语法 (例如
Fn(usize, bool) -> usize)。对此技术细节感兴趣的人可以参考 Rustonomicon 中的相关部分。
Examples
调用一个闭包
let square = |x| x * x; assert_eq!(square(5), 25);Run
使用 Fn 参数
fn call_with_one<F>(func: F) -> usize where F: Fn(usize) -> usize { func(1) } let double = |x| x * 2; assert_eq!(call_with_one(double), 2);Run
Required methods
Implementors
pub extern "rust-call" fn call(
    &self, 
    args: Args
) -> <Box<F, A> as FnOnce<Args>>::OutputⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
    I: Iterator + ?Sized,
    A: Allocator,     type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
    F: Future + Unpin + ?Sized,
    A: Allocator + 'static,     type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]
pub extern "rust-call" fn call(
    &self, 
    args: Args
) -> <Box<F, A> as FnOnce<Args>>::OutputⓘNotable traits for Box<I, A>impl<I, A> Iterator for Box<I, A> where
    I: Iterator + ?Sized,
    A: Allocator,     type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
    F: Future + Unpin + ?Sized,
    A: Allocator + 'static,     type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>
[src]Notable traits for Box<I, A>
impl<I, A> Iterator for Box<I, A> where
    I: Iterator + ?Sized,
    A: Allocator,     type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
    F: Future + Unpin + ?Sized,
    A: Allocator + 'static,     type Output = <F as Future>::Output;impl<R: Read + ?Sized> Read for Box<R>impl<W: Write + ?Sized> Write for Box<W>