标签:字符转换 参数 on() 跳过 驼峰命名 for循环 logs exp 数据
Rust中的函数采用“蛇形命名法(Snake Case)”,其函数名中只能出现小写字母和下划线_。
例如:file_name、 line_number。
调用函数示例:
fn main() {
println!("Hello, world!");
another_function();
}
fn another_function() {
println!("Another function.");
}
函数可以在main的无论前后定义。
示例:
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(没有返回值):
创建变量并绑定数值: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。
单行注释:用//开头。
多行注释:每行用//开头。
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
looploop会使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
标签:字符转换 参数 on() 跳过 驼峰命名 for循环 logs exp 数据
原文地址:https://www.cnblogs.com/wr786/p/11826737.html