变量绑定是指将一些值绑定到一个名字上,这样能够在以后使用他们。Rust 中使用 let 声明一个绑定:golang
fn main() { let x = 123; }
绑定默认是不可变的(immutable)。下面的代码将不能编译:安全
let x = 123; x = 456;
编译器会给出以下错误:this
Compiling hello_world v0.1.0 (yourpath/hello_world) error[E0384]: re-assignment of immutable variable `x` --> main.rs:3:5 | 2 | let x = 123; | - first assignment to `x` 3 | x = 456; | ^^^^^^^ re-assignment of immutable variable
若是想使得绑定是可变的,使用 mut:debug
let mut x = 123; x = 456;
若是你声明了一个可变绑定而又没有改变它,虽然能够编译经过,但编译器仍是会给出一些建议和警告:code
let mut x = 123; Compiling hello_world v0.1.0 (yourpath/hello_world) warning: unused variable: `x`, #[warn(unused_variables)] on by default --> main.rs:2:9 | 2 | let mut x = 123; | ^^^^^ warning: variable does not need to be mutable, #[warn(unused_mut)] on by default --> main.rs:2:9 | 2 | let mut x = 123; | ^^^^^ Finished debug [unoptimized + debuginfo] target(s) in 0.63 secs
这正是 Rust 出于安全的目的。orm
Rust 变量绑定有另外一个不一样于其余语言的方面:绑定要求在可使用它以前必须初始化。get
例如如下代码:编译器
fn main() { let x: i32; println!("Hello world!"); }
Rust 会警告咱们从未使用过这个变量绑定,可是由于咱们从未用过它,无害不罚。it
Compiling hello_world v0.1.0 (yourpath/hello_world) warning: unused variable: `x`, #[warn(unused_variables)] on by default --> main.rs:2:9 | 2 | let x: i32; | ^ Finished debug [unoptimized + debuginfo] target(s) in 0.29 secs
然而,若是确实想使用 x, 事情就不同了。修改代码:io
fn main() { let x: i32; println!("The value of x is: {}", x); }
这是没法经过编译的:
Compiling hello_world v0.1.0 (yourpath/hello_world) error[E0381]: use of possibly uninitialized variable: `x` --> main.rs:3:39 | 3 | println!("The value of x is: {}", x); | ^ use of possibly uninitialized `x` <std macros>:2:27: 2:58 note: in this expansion of format_args! <std macros>:3:1: 3:54 note: in this expansion of print! (defined in <std macros>) main.rs:3:5: 3:42 note: in this expansion of println! (defined in <std macros>) error: aborting due to previous error error: Could not compile `hello_world`.
Rust 不容许咱们使用一个未经初始化的绑定。
在许多语言中,这叫作变量。不过 Rust 的变量绑定有一些不一样的巧妙之处。例如 let语句的左侧是一个“模式”,而不单单是一个变量。咱们也能够这样写:
let (x, y) = (1, 2);
在这个语句被计算后,x 将会是1,'y' 将会是2。也许有人会忍不住说:这不相似于 golang 中的多变量绑定嘛。
var x, y = 1, 2
事实上不是这样的。(1, 2)这种形式是一个元组,是一个固定大小的有序列表。上面的例子还能够写成如下形式:
let n = (1, "hello"); let (x, y) = n;
第一个let将一个元组(1, "hello")绑定到n上,第二个let将元组解构,将元组中的字段绑定到x和y上。
更多模式和元组(Tuple)的特性,将在后续的章节详细介绍。
Rust 是一个静态类型语言,意味着咱们须要先肯定咱们须要的类型。事实上,Rust 有一个叫作类型推断的功能,若是它能确认这是什么类型,就不须要明确指定类型。固然也能够显示的标注类型, 类型写在一个冒号':'后面:
let x: i32 = 123;
对于数值类型,也能够这样:
let x = 123i32; // let x: i32 = 123 let y = 123_i32; // let y: i32 = 123 let z = 100_000i32; // let z: i32 = 100000