首页 > 其他分享 >[Rust] Implicitly returning values from functions

[Rust] Implicitly returning values from functions

时间:2024-02-23 15:45:37浏览次数:13  
标签:functions square i32 Implicitly num values error fn

Code has error:

fn main() {
    let answer = square(3);
    println!("The square of 3 is {}", answer);
}

fn square(num: i32) -> i32 {
    num * num;
}

Error:

⚠️  Compiling of exercises/functions/functions5.rs failed! Please try again. Here's the output:
error[E0308]: mismatched types
  --> exercises/functions/functions5.rs:12:24
   |
12 | fn square(num: i32) -> i32 {
   |    ------              ^^^ expected `i32`, found `()`
   |    |
   |    implicitly returns `()` as its body has no tail or `return` expression
13 |     num * num;
   |              - help: remove this semicolon to return this value

error: aborting due to previous error

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

 

If we do:

fn square(num: i32) -> i32 {
    num * num
}

That will works, because without ;, Rust consider that line as implicity return value, if you have ;, you need to add returnkeyword:

fn square(num: i32) -> i32 {
    return num * num;
}

 

标签:functions,square,i32,Implicitly,num,values,error,fn
From: https://www.cnblogs.com/Answer1215/p/18029700

相关文章

  • Object — Object.values()
    Object.values()是JavaScript中的一个内置方法,用于返回一个包含指定对象所有可枚举属性值的数组。Object.values()方法会遍历目标对象的可枚举属性(不包括继承的属性),并返回一个数组,该数组包含了这些属性的值。 1letobj={2name:'red',3age:'18',4......
  • 绕过disable_functions的限制
    https://github.com/AntSwordProject/AntSword-Labs/tree/master/bypass_disable_functionshttps://wiki.luoyunhao.com/web/Bypassdisable_function绕过disable_functions的限制disable_functions是php.ini中的一个设置选项,可以用来设置PHP环境禁止使用某些函数,通常是网站......
  • 适用于 Amazon Step Functions 的低代码可视化新工作流 Workflow Studio, 现已在 Amaz
    今天,我们非常欣喜地宣布现已在AmazonApplicationCompose中推出AmazonStepFunctionsWorkflowStud。通过这款全新的集成应用,工作流与应用程序资源开发便可整合到统一的可视化基础设施即代码(IaC)生成器。对于使用AmazonStepFunctionsWorkflowStudio创建工作流与......
  • CF1879D Sum of XOR Functions
    记前缀异或和数组\(s\),于是题目中的\(f(l,r)\)可以被表示成\(s_r\opluss_{l-1}\),转化为求\(\sum\limits_{l=1}^n\sum\limits_{r=l}^n(s_r\opluss_{l-1})\times(r-l+1)\)。再记录\(4\)个值,\(cnt_0,cnt_1,sum_0,sum_1\)分别表示当前已经出现过的\(0/1\)的个数,出......
  • [Rust] Macros vs. Functions
    InRust,theexclamationmark(!)afteranameindicatesthatitisamacroratherthanafunction.MacrosandfunctionsinRustarecalleddifferentlyandservedifferentpurposes.Macros,likeanyhow!,candothingsthatfunctionscannot,suchasgenera......
  • ChessFunctions+ActiveXControl+SharedAddIn三合一【Office和VBA中呈现中国象棋】
    本软件由三个项目构成,各自下载链接如下:ChessFunctions链接:https://pan.baidu.com/s/11pMnmd28nHtpTGCU9rwNHg提取码:1234ChessFunctions的帮助文件链接:https://pan.baidu.com/s/1uxJYx8gOd8sNEBlda3onnA提取码:1234ActiveXControl链接:https://pan.baidu.com/s/1CTLcXlQgZaD1_av......
  • Flink之状态编程 值状态(ValueState)列表状态(ListState)映射状态(MapState)归约状态(Reducin
    Flink之状态编程值状态(ValueState)列表状态(ListState)映射状态(MapState)归约状态(ReducingState)聚合状态(AggregatingState)广播状态(BroadcastState)Flink之状态编程一、按键分区状态(KeyedState)1.1、值状态(ValueState)1.1.1、定义1.1.2、使用案例1.2、列表状态(ListState)1.2.1......
  • JS遍历对象的方法 Object.keys() Object.values()
    1.Object.keys():返回对象可枚举属性组成的数据2.Object.values():返回对象可枚举的属性的属性值,组成的数据letperson={name:'小李',age:'15',}console.log(Object.keys(person));//['name','age']//返回对象可枚举属性组成的数......
  • net8字符串匹配查找System.Buffers.SearchValues类
    新增的System.Buffers.SearchValues类,可以用来进行字符串的查找和匹配,相比较 string 类型的操作,性能有大幅提升,下面还是用BenchmarkDotNet进行测试:BenchmarkRunner.Run<SearchValuesTest>();Console.ReadKey();[SimpleJob(RunStrategy.ColdStart,iterationCount:5)]......
  • C# .NET 中 LINQ to Entities查询中使用时转换为数据库函数的CLR方法(EF.Functions)
    EF.Functions映射由于并非所有数据库函数都有等效的C#函数,因此EFCore提供程序提供了特殊的C#方法来调用某些数据库函数。这些方法通过EF.Functions定义为扩展方法来用于LINQ查询中。这些方法是特定于提供程序的,因为它们与特定数据库函数密切相关。因此,适用于某个......