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

推薦閱讀
- JavaScript從入門到精通(微視頻精編版)
- 數據庫程序員面試筆試真題與解析
- TensorFlow Lite移動端深度學習
- Arduino by Example
- Scratch 3游戲與人工智能編程完全自學教程
- MATLAB定量決策五大類問題
- Kali Linux Wireless Penetration Testing Beginner's Guide(Third Edition)
- Kinect for Windows SDK Programming Guide
- UML 基礎與 Rose 建模案例(第3版)
- Visual Studio Code 權威指南
- Python計算機視覺和自然語言處理
- 微前端設計與實現
- Mastering OpenStack
- JavaScript Unit Testing
- Distributed Computing with Python