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

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:

主站蜘蛛池模板: 汉阴县| 定兴县| 彰化市| 景东| 原阳县| 武平县| 阿瓦提县| 临洮县| 从化市| 太原市| 江津市| 鄂伦春自治旗| 庄浪县| 肥西县| 大关县| 建平县| 象州县| 孝义市| 固阳县| 宁化县| 孝感市| 普格县| 渭南市| 临泽县| 华宁县| 贡嘎县| 昌宁县| 保德县| 都安| 新河县| 汾阳市| 彭山县| 建瓯市| 大同县| 邛崃市| 依安县| 申扎县| 蒙山县| 高密市| 屯门区| 四川省|