标识符
The first character is a letter.
The remaining characters are alphanumeric or _.
或
The first character is _.
The identifier is more than one character. _ alone is not an identifier.
The remaining characters are alphanumeric or _.
如果想使用Rust的关键字作为标识符,则需要以r#为前缀
Raw identifiers
Sometimes, you may need to use a name that’s a keyword for another purpose. Maybe you need to call a function named match that is coming from a C library, where ‘match’ is not a keyword. To do this, you can use a “raw identifier.” Raw identifiers start with r#:
let r#fn = "this variable is named 'fn' even though that's a keyword"; // call a function named 'match' r#match();
2.1 variables and mutability
cargo new variables
[root@itoracle test]# vim variables/src/main.rs fn main() { let x = 5; println!("The value of x is: {}", x); x = 6; println!("The value of x is: {}", x); }
以上代码将在编译时报错,因为rust默认变量是不能对其值修改的。以下代码是正确的
fn main() { let mut x = 5; println!("The value of x is: {}", x); x = 6; println!("The value of x is: {}", x); }
[root@itoracle test]# cd variables/ [root@itoracle variables]# cargo run Compiling variables v0.1.0 (/usr/local/automng/src/rust/test/variables) Finished dev [unoptimized + debuginfo] target(s) in 3.15s Running `target/debug/variables` The value of x is: 5 The value of x is: 6
变量与常量
区别:常量必须在编辑时就确定其值,变量可以是表达式,可以在运行时确定其值。
First, you aren’t allowed to use mut
with constants. Constants aren’t just immutable by default—they’re always immutable.
You declare constants using the const
keyword instead of the let
keyword, and the type of the value must be annotated. Just know that you must always annotate the type.
Constants can be declared in any scope, including the global scope, which makes them useful for values that many parts of code need to know about.
The last difference is that constants may be set only to a constant expression, not the result of a function call or any other value that could only be computed at runtime.
Here’s an example of a constant declaration where the constant’s name is MAX_POINTS
and its value is set to 100,000. (Rust’s naming convention for constants is to use all uppercase with underscores between words, and underscores can be inserted in numeric literals to improve readability):
2.4 注释
All programmers strive to make their code easy to understand, but sometimes extra explanation is warranted. In these cases, programmers leave notes, or comments, in their source code that the compiler will ignore but people reading the source code may find useful.
单行注释//
fn main() { let lucky_number = 7; // I’m feeling lucky today }
fn main() { // I’m feeling lucky today let lucky_number = 7; }
2.5 流程控制
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"); } }
if 中使用 let 语句
要求 if 条件各个分支返回的数据类型必须相同
fn main() { let condition = true; let number = if condition { 5 } else { 6 }; println!("The value of number is: {}", number); }
loop循环
无返回值
fn main() { let mut count = 1; loop{ count += 1; if count == 8{ break; } } }
有返回值
fn main() { let mut counter = 0; let result = loop { counter += 1; if counter == 10 { break counter * 2; } }; //assert_eq!方法可以在result的值不为20时抛出异常 assert_eq!(result, 20); }
比如上面的方法,如果写成 assert_eq!(result, 19);
则在运行时会给出以下错误信息
[root@itoracle functions]# cargo run Compiling functions v0.1.0 (/usr/local/automng/src/rust/test/functions) Finished dev [unoptimized + debuginfo] target(s) in 1.80s Running `target/debug/functions` thread 'main' panicked at 'assertion failed: `(left == right)` left: `20`, right: `19`', src/main.rs:12:5 note: Run with `RUST_BACKTRACE=1` for a backtrace.
while循环
fn main() { let mut number = 3; while number != 0 { println!("{}!", number); number = number - 1; } println!("LIFTOFF!!!"); }
fn main() { let a = [10, 20, 30, 40, 50]; let mut index = 0; while index < 5 { println!("the value is: {}", a[index]); index = index + 1; } }
for循环
相比上面while的方法,for不用关心数组的长度
fn main() { let a = [10, 20, 30, 40, 50]; for element in a.iter() { println!("the value is: {}", element); } }
for数字遍历
fn main() { for number in (1..4).rev() { println!("{}!", number); } println!("LIFTOFF!!!"); }
[root@itoracle functions]# cargo run Compiling functions v0.1.0 (/usr/local/automng/src/rust/test/functions) Finished dev [unoptimized + debuginfo] target(s) in 1.62s Running `target/debug/functions` 3! 2! 1! LIFTOFF!!!
fn main() { for number in 1..4 { println!("{}!", number); } println!("LIFTOFF!!!"); }
[root@itoracle functions]# cargo run Compiling functions v0.1.0 (/usr/local/automng/src/rust/test/functions) Finished dev [unoptimized + debuginfo] target(s) in 2.09s Running `target/debug/functions` 1! 2! 3! LIFTOFF!!!