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

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:

主站蜘蛛池模板: 九龙坡区| 廉江市| 屯昌县| 焦作市| 陆丰市| 米易县| 桃源县| 常德市| 鱼台县| 三明市| 芦溪县| 赤峰市| 海林市| 香港 | 康乐县| 娄烦县| 从江县| 英山县| 焦作市| 鲜城| 南安市| 德保县| 徐汇区| 东乡族自治县| 柳江县| 伊金霍洛旗| 阿拉善左旗| 阿坝| 弋阳县| 渑池县| 时尚| 普宁市| 阿尔山市| 宝丰县| 息烽县| 北安市| 武山县| 康平县| 石城县| 虹口区| 黑河市|