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

推薦閱讀
- CockroachDB權(quán)威指南
- R語(yǔ)言編程指南
- 21天學(xué)通C++(第6版)
- 名師講壇:Java微服務(wù)架構(gòu)實(shí)戰(zhàn)(SpringBoot+SpringCloud+Docker+RabbitMQ)
- 深入淺出Serverless:技術(shù)原理與應(yīng)用實(shí)踐
- 數(shù)據(jù)結(jié)構(gòu)與算法分析(C++語(yǔ)言版)
- Protocol-Oriented Programming with Swift
- 第一行代碼 C語(yǔ)言(視頻講解版)
- MySQL入門(mén)很輕松(微課超值版)
- Java EE企業(yè)級(jí)應(yīng)用開(kāi)發(fā)教程(Spring+Spring MVC+MyBatis)
- STM8實(shí)戰(zhàn)
- AI自動(dòng)化測(cè)試:技術(shù)原理、平臺(tái)搭建與工程實(shí)踐
- 算法秘籍
- Java EE 程序設(shè)計(jì)
- Node.js核心技術(shù)教程