官术网_书友最值得收藏!

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

主站蜘蛛池模板: 广南县| 康马县| 喀什市| 平原县| 东乡县| 鄂托克旗| 扎赉特旗| 南部县| 民丰县| 台北县| 习水县| 西峡县| 翼城县| 波密县| 宁乡县| 新巴尔虎左旗| 霞浦县| 和政县| 凤城市| 宁南县| 杭锦后旗| 武冈市| 桃园县| 缙云县| 潼南县| 涟水县| 从江县| 乌拉特中旗| 临泽县| 平定县| 江安县| 汽车| 西丰县| 洱源县| 佛坪县| 洪洞县| 克拉玛依市| 龙泉市| 玉环县| 乌兰察布市| 板桥市|