函数
fn main() {println!("Hello, world!");another_function();}fn another_function() {println!("Another function.");}
函数参数
在函数签名中,必须 声明每个参数的类型。
fn main() {another_function(5);}fn another_function(x: i32) {println!("The value of x is: {}", x);}// 多个参数fn main() {another_function(5, 6);}fn another_function(x: i32, y: i32) {println!("The value of x is: {}", x);println!("The value of y is: {}", y);}
有返回值的函数
fn five() -> i32 {5}fn main() {let x = five();println!("The value of x is: {}", x);}-------------------------------------fn main() {let x = plus_one(5);println!("The value of x is: {}", x);}fn plus_one(x: i32) -> i32 {x + 1}
语句和表达式
Rust 是一门基于表达式(expression-based)的语言。
语句(Statements)是执行一些操作但不返回值的指令。
表达式(Expressions)计算并产生一个值。
有”;”结尾的是语句,不返回值。
没有”;”结尾的是表达式,返回值。
// 语句没有返回值fn main() {let y = 6;}------------------------fn main() {let x = 5;// 表达式有返回值let y = {let x = 3;x + 1};println!("The value of y is: {}", y);}// ------- 这是一个表达式 start{let x = 3;x + 1}// ------- 这是一个表达式 end
