2. Rust概念

标识符

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):

[root@itoracle variables]# vim src/main.rs 

fn main() {
    let mut x = 5;
    println!("The value of x is: {}", x);
    x = 6;
    println!("The value of x is: {}", x);
    const MAX_POINTS: u32 = 100_000;
    println!("常量:{}",MAX_POINTS)
}
[root@itoracle variables]# cargo run
   Compiling variables v0.1.0 (/usr/local/automng/src/rust/test/variables)                                               
    Finished dev [unoptimized + debuginfo] target(s) in 1.10s                                                            
     Running `target/debug/variables`
The value of x is: 5
The value of x is: 6
常量:100000

猜你喜欢

转载自www.cnblogs.com/perfei/p/10303242.html
2.