首页 > 其他分享 >[Rust] Macros vs. Functions

[Rust] Macros vs. Functions

时间:2024-02-15 16:22:53浏览次数:35  
标签:Functions macro syntax Macros vs anyhow Rust

In Rust, the exclamation mark (!) after a name indicates that it is a macro rather than a function. Macros and functions in Rust are called differently and serve different purposes. Macros, like anyhow!, can do things that functions cannot, such as generating code based on the input parameters at compile time, which is why they have a different syntax for invocation.

 

Macros vs. Functions

  • Macros (anyhow!): Macros perform operations at compile time before the final binary is produced. They can take a variable number of arguments, and their implementation can generate code based on those arguments. The ! is mandatory to invoke a macro and is part of its syntax. Without the !, Rust will not recognize it as a macro call and will instead look for a function or another item with the provided name, resulting in a compilation error if no such item exists.

  • Functions: Functions are called without an exclamation mark and perform operations at runtime. They have a fixed signature, meaning they take a specified number of arguments with defined types and return a specific type.

 

What Happens Without the !

If you try to use anyhow without the !, like this:

let error = anyhow("An error occurred");

Rust will attempt to find a function or another item named anyhow, not a macro. Since no such function exists in the anyhow crate (the main feature is the macro for creating errors), this will result in a compilation error.

 

Conclusion

Yes, without the !, calling the anyhow macro won't work because Rust's syntax requires the ! to identify and invoke macros. This syntax rule helps maintain clarity and distinction between macros and functions within Rust's ecosystem.

标签:Functions,macro,syntax,Macros,vs,anyhow,Rust
From: https://www.cnblogs.com/Answer1215/p/18016318

相关文章

  • PCIe 3.0 vs 2.0 – What’s the Difference?
    PCIe3.0vs2.0–What’stheDifference?https://www.technewstoday.com/pcie-3-0-vs-2-0/https://www.technewstoday.com/pcie-3-0-vs-2-0/LearnaboutoureditorialpoliciesUpdatedDecember11,2022Whenbuyingamotherboardoragraphicscard,haveyouev......
  • VSCode的configuration错误
    问题首先通过报错语句,定位到错误代码。通过上图的报错语句,定位到了我的错误代码在train.py文件中parser.add_argument方法的问题,截图如下:  通过百度parser.add_argument语句,发现当required参数为True时,在运行时必须要为该命令指定路径。从上图可以看到第一条parser.add_ar......
  • NET项目&DLL反编译&MSSQL监控&VS搜索&注入&上传
    知识点1.NET普通源码&编译源码2.DLL反编译&后缀文件&指向3.代码审计-SQL注入&文件上传ASPX文件->CSASPX.CSDLL反编译后寻找看核心代码分析漏洞CSASPX.CSDLL反编译文件->ASPX文件寻找确定漏洞进行调试测试代码审计时要把这个反编译文件提取导入到IDE中后期搜索关......
  • 文心一言 VS 讯飞星火 VS chatgpt (198)-- 算法导论14.3 6题
    六、用go语言,说明如何来维护一个支持操作MIN-GAP的一些数的动态集Q,使得该操作能给出Q中两个最接近的数之间的差值。例如,Q=(1,5,9,15,18,22),则MIN-GAP返回18-15=3,因为15和18是Q中两个最接近的数。要使得操作INSERT、DELETE、SEARCH和MIN-GAP尽可能高效,并分析它们的运行时间。文心一言,代......
  • VS Code
    VSCode创建文件的方式:新建文件夹,之后(1)拖到VSCode上打开这个文件夹。(2)用VSCode的菜单选择打开文件夹。两种方式都可以。■VSCode布局常用按钮,熟悉一下。■自动生成代码:!+Tab键:自动生成html内容。emment快捷键:ul.ul-list>li.li-${li_$}*5a.img-link[hre......
  • 文心一言 VS 讯飞星火 VS chatgpt (197)-- 算法导论14.3 5题
    五、用go语言,对区间树T和一个区间i,请修改有关区间树的过程来支持新的操作INTERVALSEARCH-EXACTLY(T,i),它返回一个指向T中结点x的指针,使得x.int.low==i.low且x.int.high==i.high;或者,如果不包含这样的区间时返回T.nil。所有的操作(包括INTERVAL-SEARCH-EXACTLY)......
  • VS2022+OpenCV_contrib安装
    准备:Cmake,OpenCV安装包,OpenCV扩展包安装步骤:一:OpenCV扩展包编译打开文件夹新建一个文件夹 打开cmake开始编译第一栏Whereisthesourcecode是指OpenCV解压后得到的source文件的路径;第二栏wheretobuildthebinaries是指编译后输出文件的路径,直接在opencv的同个大文件......
  • 前端vscode+eslint代码规范
    前端使用vscode+eslint格式化规范代码格式在应用商店安装eslint,配置好eslint,根目录新增.eslintrc.jsmodule.exports={root:true,parserOptions:{parser:'babel-eslint',sourceType:'module'},env:{browser:true,node:true,es6......
  • 02 在vscode中使用python
    安装插件需要先安装python这个插件安装完成后,创建一个文件夹,用于工程的创建。使用vscode打开这个文件夹,之后新建一个.py文件。编写第一个程序:print("hello")a=3b=4print(a+b)box="gogogo"name="lili"print("byby"+name)配置相关信息选择这个:......
  • 在K8S中,kube-proxy ipvs 原理是什么?
    在Kubernetes(K8s)集群中,kube-proxy是一个关键组件,它负责实现从Service到后端Pods的网络代理和负载均衡功能。当kube-proxy工作在IPVS模式时,其原理如下:监听API服务器:kube-proxy启动后会持续监听KubernetesAPI服务器上的Service资源对象的变化。每当有新的Service创建、更......