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

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:

主站蜘蛛池模板: 盘山县| 永福县| 监利县| 白河县| 泽库县| 五家渠市| 小金县| 获嘉县| 滨海县| 保德县| 天柱县| 射洪县| 香河县| 商洛市| 祁门县| 尖扎县| 富阳市| 大同市| 南雄市| 手机| 郓城县| 通许县| 怀化市| 阿尔山市| 江北区| 邵武市| 剑川县| 穆棱市| 渑池县| 自贡市| 建水县| 天津市| 甘南县| 桂阳县| 台前县| 长垣县| 久治县| 墨竹工卡县| 磐安县| 郸城县| 义乌市|