- 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:

推薦閱讀
- 圖解Java數據結構與算法(微課視頻版)
- Mastering QGIS
- PostgreSQL技術內幕:事務處理深度探索
- Python程序設計
- Hands-On GPU:Accelerated Computer Vision with OpenCV and CUDA
- Java系統化項目開發教程
- Linux Shell核心編程指南
- 一步一步跟我學Scratch3.0案例
- Mastering ASP.NET Core 2.0
- SCRATCH編程課:我的游戲我做主
- 軟硬件綜合系統軟件需求建模及可靠性綜合試驗、分析、評價技術
- C語言程序設計
- Raspberry Pi開發實戰
- 軟件測試
- Storm Real-Time Processing Cookbook