- Learning Rust
- Paul Johnson Vesa Kaihlavirta
- 163字
- 2021-07-02 23:07:19
Passing by value
The following code shows how to pass a number between two functions, and to receive a result:
fn main() { let add = add_values(3, 5); println!("{:?}", add); } fn add_values(a: i32, b: i32) -> i32 { a + b }
Let's have a look at the receiving function's definition line:
fn add_values(a: i32, b: i32) -> i32
As with any programming language, we have to give the function a name, and then a parameter list. The parameter names are followed by a colon and the type of the parameter.
Our function returns a value (this is signified by the -> symbol) of a particular type (in this case, i32). The last evaluated thing in the function will be returned from the function, provided that you don't accidentally put a semi-colon there. An implicit return statement also exists, but it's not required and it's usually better style to omit it if possible.
When built and run, you will see the following:

推薦閱讀
- 計算思維與算法入門
- CMDB分步構建指南
- R語言經典實例(原書第2版)
- Spring Cloud Alibaba微服務架構設計與開發實戰
- 營銷數據科學:用R和Python進行預測分析的建模技術
- Android 應用案例開發大全(第3版)
- Java EE 7 Performance Tuning and Optimization
- Mastering Data Mining with Python:Find patterns hidden in your data
- LabVIEW虛擬儀器入門與測控應用100例
- FFmpeg開發實戰:從零基礎到短視頻上線
- C語言程序設計與應用實驗指導書(第2版)
- 量子計算機編程:從入門到實踐
- Puppet 5 Beginner's Guide(Third Edition)
- C語言程序設計
- SQL Server 2012數據庫管理與開發(慕課版)