RUST 0x02 函数与注释与控制流
1 函数
Rust中的函数采用“蛇形命名法(Snake Case)”,其函数名中只能出现小写字母和下划线_
。
例如:file_name、 line_number。
调用函数示例:
fn main() { println!("Hello, world!"); another_function(); } fn another_function() { println!("Another function."); }
函数可以在main
的无论前后定义。
函数参数(Parameters)
示例:
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); }
在定义函数时应注意声明参数的数据类型。
多个参数间用
,
分隔。不同参数的数据类型不必相同。
函数体包括语句和表达式(Statements&Expressions)
Statements(没有返回值):
创建变量并绑定数值:
let
所以
let x = (let y = 6);
是错误的。定义函数
Expressions:
数字运算:
5 + 6
{}
let y = { let x = 3; x + 1 };
需要注意的是,这里的
x+1
的后面没有;
,因为如果加了;
,整个{}
将变为一个Statement,从而不具有返回值。
有返回值的函数
函数能够具有返回值。
返回值不用命名,但是需要用
->
声明数据类型。在Rust中,函数的返回值等于函数体中的最后一个表达式。
也可以用
return
提前返回值。
示例:
fn five() -> i32 { 5 } fn main() { let x = five(); println!("The value of x is: {}", x); }
如果在应有返回值的函数体的最后一个表达式尾加入;
,则会抛出一个CE。
2 注释
单行注释:用//
开头。
多行注释:每行用//
开头。
3 控制流
if
表达式
示例:
fn main() { let number = 3; if number < 5 { println!("condition was true"); } else { println!("condition was false"); } }
if
→ ifelse
→ else
需要注意的是,if
后面的条件语句最好不加()
,否则会抛出一个warning。
且条件语句的值必须是一个bool
,否则会抛出一个CE。Rust并不会自动将整数类型转换为逻辑类型。所以如果要判断一个数字非零,运用!= 0
。
利用else if
处理多条件情况
示例:
fn main() { let number = 6; if number % 4 == 0 { println!("number is divisible by 4"); } else if number % 3 == 0 { println!("number is divisible by 3"); } else if number % 2 == 0 { println!("number is divisible by 2"); } else { println!("number is not divisible by 4, 3, or 2"); } }
Rust只执行最先条件为真的分支,在其后面的分支将会被跳过。
在let
语句中运用if
因为if
是一个表达式,所以我们可以在let
语句的右边运用if
示例:
fn main() { let condition = true; let number = if condition { 5 } else { 6 }; println!("The value of number is: {}", number); }
需要注意的是,在这种情况下,每个if
分支中的表达式的数据类型必须是相同的。否则会抛出一个CE。
Rust中有三种循环方式:loop
、while
、for
loop
loop
会使Rust不断重复执行一段代码,直到你让它停止。
示例:
fn main() { loop { println!("again!"); } }
要使println!("again!");
停止,可以关闭程序,或者按^C
(Ctrl+C
)中止程序。
也可以在loop
中加入break
来停止执行loop
。
在loop
中返回值
可以在break
之后加上想要返回的值,例如:
fn main() { let mut counter = 0; let result = loop { counter += 1; if counter == 10 { break counter * 2; } }; println!("The result is {}", result); }
条件循环while
示例:
fn main() { let mut number = 3; while number != 0 { println!("{}!", number); number -= 1; } println!("LIFTOFF!!!"); }
同样地,最好不要加()
,否则会抛出一个warning。
利用for
循环遍历一个集合
我们可以用while
来遍历一个集合:
fn main() { let a = [10, 20, 30, 40, 50]; let mut index = 0; while index < 5 { println!("the value is: {}", a[index]); index += 1; } }
但是这样写是易于出错的(下标可能越界),而且还比较慢(每一个循环都要判断一下条件)。
用for
可以更简洁:
fn main() { let a = [10, 20, 30, 40, 50]; for element in a.iter() { println!("the value is: {}", element); } }
iter()
→ 迭代器(iterator)
fn main() { for number in (1..4).rev() { println!("{}!", number); } println!("LIFTOFF!!!"); }
(1..4)
→Range
类型,生成一个序列"1 2 3"(左闭右开)。rev()
→ 反转(reverse) 。
参考
The Rust Programming Language by Steve Klabnik and Carol Nichols, with contributions from the Rust Community : https://doc.rust-lang.org/book/
蛇形命名法(snake case)驼峰命名法(camel case)字符转换问题 by 王约翰的网络日志:https://www.cnblogs.com/wangyuehan/p/10079855.html