首页 > 其他分享 >Closure as the function parameter

Closure as the function parameter

时间:2023-06-15 16:35:38浏览次数:73  
标签:Closure function functions closure pointer foo parameter fn


Closure as the function parameter

(Jin Qing’s Column, Mar., 2022)

It is best to let the function take a closure trait as the parameter instead of a function pointer.

fn foo(f: fn()) {
    f()
}

fn main() {
    foo(|| println!("hello"));
    let a = 123;
    foo(move || println!("{}", a))
}

compiles error:

error[E0308]: mismatched types
 --> src/main.rs:9:9
  |
9 |     foo(move || println!("{}", a))
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^ expected fn pointer, found closure
  |
  = note: expected fn pointer `fn()`
                found closure `[closure@src/main.rs:9:9: 9:34]`
note: closures can only be coerced to `fn` types if they do not capture any variables
 --> src/main.rs:9:32
  |
9 |     foo(move || println!("{}", a))
  |                                ^ `a` captured here

For more information about this error, try `rustc --explain E0308`.

https://doc.rust-lang.org/book/ch19-05-advanced-functions-and-closures.html

Function pointers implement all three of the closure traits (Fn, FnMut, and FnOnce), so you can always pass a function pointer as an argument for a function that expects a closure. It’s best to write functions using a generic type and one of the closure traits so your functions can accept either functions or closures.


标签:Closure,function,functions,closure,pointer,foo,parameter,fn
From: https://blog.51cto.com/u_16162321/6493187

相关文章

  • [从jQuery看JavaScript]-匿名函数与闭包(Anonymous Function and Closure)
    jQuery片段:1.(function(){2.//这里忽略jQuery所有实现3.})();当一个匿名函数被括起来,然后再在后面加一个括号,这个匿名函数就能立即运行起来!真神奇哦!嘿嘿!胡闹到此为止。在这一节,我们碰到的jQuery片段是一组立即运行的匿名函数。而这种用法在论坛上也曾引起过激辩......
  • 【Azure 应用服务】Azure Function App在部署时候遇见 503 ServiceUnavailable
    问题描述在VSCode中编写好AzureFunctionApp代码后,通过 funcazurefunctionapppublish部署失败,抛出503ServiceUnavailable错误。Gettingsitepublishinginfo...Creatingarchiveforcurrentdirectory...Performingremotebuildforfunctionsproject.Deleting......
  • opcenter camstar designer基础知识-- Functions
     已编写函数来执行各种任务,如简单的算术、搜索复杂的数据结构、数据库查询、报告编写、数据收集、数据验证等等。函数具有零个或更多参数,并且由ActiveX组件实施。包含在CLF中的函数可以执行工作。函数有权访问系统的内部对象设计,它通过操纵该设计来完成工作。以下主题提供有......
  • 【Azure 应用服务】Azure Function App在部署时候遇见 503 ServiceUnavailable
    问题描述在VSCode中编写好AzureFunctionApp代码后,通过 funcazurefunctionapppublish部署失败,抛出503ServiceUnavailable错误。Gettingsitepublishinginfo...Creatingarchiveforcurrentdirectory...Performingremotebuildforfunctionsproject.Dele......
  • 【JS基础】Function构造函数
    Function()构造函数创建了一个新的Function对象,直接调用构造函数可以动态创建函数,与eval(可能访问到本地作用域)不同的是,Function构造函数只创建全局执行的函数。constsum=newFunction('a','b','returna+b')console.log(sum(1,2)); 参考:Function()构造函数-J......
  • 机器学习模型中的损失函数loss function
    1.概述在机器学习算法中,有一个重要的概念就是损失函数(LossFunction)。损失函数的作用就是度量模型的预测值与真实值之间的差异程度的函数,且是一个非负实值函数。对于分类问题损失函数通常可以表示成损失项和正则项的和,即有如下的形式:其中,为损失项,为正则项。的具体形式如下:对于损失......
  • 【Azure 应用服务】Azure Data Factory中调用Function App遇见403 - Forbidden
    问题描述在AzureDataFactory(数据工厂)中,调用同在Azure中的FunctionApp函数,却出现403-Forbidden错误。截图如下:  问题解答访问AzureFunctionApp遇见403-Forbidden错误,这是因为FunctionApp启用了限制访问功能,在其中配置了允许访问的IP地址列表,而从ADF中发出的请求使用的I......
  • 【Azure 应用服务】Azure Data Factory中调用Function App遇见403 - Forbidden
    问题描述在AzureDataFactory(数据工厂)中,调用同在Azure中的FunctionApp函数,却出现403-Forbidden错误。截图如下:  问题解答访问AzureFunctionApp遇见403-Forbidden错误,这是因为FunctionApp启用了限制访问功能,在其中配置了允许访问的IP地址列表,而从ADF中发出的请......
  • KubeSphere 社区双周报 | OpenFunction 发布 v1.1.0 | 2023.5.26-6.8
    KubeSphere社区双周报主要整理展示新增的贡献者名单和证书、新增的讲师证书以及两周内提交过commit的贡献者,并对近期重要的PR进行解析,同时还包含了线上/线下活动和布道推广等一系列社区动态。本次双周报涵盖时间为:2023.05.26-2023.06.08。贡献者名单新晋KubeSphereCon......
  • jquery:TypeError: $(...).on is not a function
    当发生错误TypeError:$(...).onisnotafunction时,当不存在javascript类冲突的前提下,考虑是否是因为jquery版本过低! 参考:http://stackoverflow.com/questions/15670352/typeerror-on-is-not-a-functionhttp://stackoverflow.com/questions/18958775/typeerror-jquery-on-is-n......