init code
This commit is contained in:
7
4.Variable Bindings/4. Varable Bindings/Cargo.lock
generated
Normal file
7
4.Variable Bindings/4. Varable Bindings/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "veriable_bindings"
|
||||
version = "0.1.0"
|
||||
8
4.Variable Bindings/4. Varable Bindings/Cargo.toml
Normal file
8
4.Variable Bindings/4. Varable Bindings/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "veriable_bindings"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
19
4.Variable Bindings/4. Varable Bindings/src/main.rs
Normal file
19
4.Variable Bindings/4. Varable Bindings/src/main.rs
Normal file
@@ -0,0 +1,19 @@
|
||||
fn main() {
|
||||
let an_integer = 1u32;
|
||||
let a_boolean = true;
|
||||
let unit = ();
|
||||
|
||||
// 储存在栈上的基础值,会直接复制
|
||||
let copied_integer = an_integer;
|
||||
|
||||
println!("An integer: {:?}", copied_integer);
|
||||
println!("A boolean: {:?}", a_boolean);
|
||||
println!("Meet the unit value: {:?}", unit);
|
||||
|
||||
// 没有被使用的变量编译器会发出警告,可以通过在变量前面添加一个下划线来禁用编译器警告
|
||||
let _unused_variable = 3u32;
|
||||
|
||||
let noisy_unused_variable = 2u32;
|
||||
// FIXME ^ 在变量名前面添加下划线来禁用警告
|
||||
// PS:网页的在线测试看不到警告信息
|
||||
}
|
||||
7
4.Variable Bindings/4.1 Mutability/Cargo.lock
generated
Normal file
7
4.Variable Bindings/4.1 Mutability/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "mutability"
|
||||
version = "0.1.0"
|
||||
8
4.Variable Bindings/4.1 Mutability/Cargo.toml
Normal file
8
4.Variable Bindings/4.1 Mutability/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "mutability"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
14
4.Variable Bindings/4.1 Mutability/src/main.rs
Normal file
14
4.Variable Bindings/4.1 Mutability/src/main.rs
Normal file
@@ -0,0 +1,14 @@
|
||||
fn main() {
|
||||
let _immutable_binding = 1;
|
||||
let mut mutable_binding = 1;
|
||||
|
||||
println!("Before mutation: {}", mutable_binding);
|
||||
|
||||
// 可变声明,所以可以修改
|
||||
mutable_binding += 1;
|
||||
|
||||
println!("After mutation: {}", mutable_binding);
|
||||
|
||||
// 报错! 不可变声明不能修改内容
|
||||
_immutable_binding += 1;
|
||||
}
|
||||
7
4.Variable Bindings/4.2 Scope and Shadowing/Cargo.lock
generated
Normal file
7
4.Variable Bindings/4.2 Scope and Shadowing/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "scope_and_shadowing"
|
||||
version = "0.1.0"
|
||||
8
4.Variable Bindings/4.2 Scope and Shadowing/Cargo.toml
Normal file
8
4.Variable Bindings/4.2 Scope and Shadowing/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "scope_and_shadowing"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
45
4.Variable Bindings/4.2 Scope and Shadowing/src/main.rs
Normal file
45
4.Variable Bindings/4.2 Scope and Shadowing/src/main.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
fn example01() {
|
||||
// 在当前函数作用域中 绑定 1 到变量 long_lived_binding
|
||||
let long_lived_binding = 1;
|
||||
|
||||
// 这个是一个内部嵌套的小范围作用域
|
||||
{
|
||||
// 这个变量声明只在当前的小范围内生效
|
||||
let short_lived_binding = 2;
|
||||
|
||||
println!("inner short: {}", short_lived_binding);
|
||||
}
|
||||
// 嵌套的作用域结束,该作用域内的全部变量都会被回收
|
||||
|
||||
// 报错! `short_lived_binding` 这个变量已经被回收了
|
||||
println!("outer short: {}", short_lived_binding);
|
||||
// FIXME ^ 注释这行代码
|
||||
|
||||
println!("outer long: {}", long_lived_binding);
|
||||
}
|
||||
|
||||
fn example02() {
|
||||
let shadowed_binding = 1;
|
||||
|
||||
{
|
||||
// 这里使用的变量值是外面的函数作用域的值
|
||||
println!("before being shadowed: {}", shadowed_binding);
|
||||
|
||||
// 这里重新定义了一个同名的变量,进行了变量的遮蔽(shadowing)
|
||||
let shadowed_binding = "abc";
|
||||
|
||||
println!("shadowed in inner block: {}", shadowed_binding);
|
||||
}
|
||||
// 小作用域已经结束,这里的变量使用的依旧是函数作用域的值
|
||||
println!("outside inner block: {}", shadowed_binding);
|
||||
|
||||
// 这里对上面定义的变量重新定义,遮蔽(shadowing)了前面的变量声明
|
||||
let shadowed_binding = 2;
|
||||
println!("shadowed in outer block: {}", shadowed_binding);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
example01();
|
||||
example02();
|
||||
println!("Hello, world!");
|
||||
}
|
||||
7
4.Variable Bindings/4.3 Declare first/Cargo.lock
generated
Normal file
7
4.Variable Bindings/4.3 Declare first/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "declare_first"
|
||||
version = "0.1.0"
|
||||
8
4.Variable Bindings/4.3 Declare first/Cargo.toml
Normal file
8
4.Variable Bindings/4.3 Declare first/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "declare_first"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
30
4.Variable Bindings/4.3 Declare first/src/main.rs
Normal file
30
4.Variable Bindings/4.3 Declare first/src/main.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
//!
|
||||
//! 变量的定义和初始化通常是同时进行的。
|
||||
//! Rust 允许把两个操作分开,先定义,后初始化
|
||||
//! 使用变量的前提条件是变量必须被初始化了以后才能使用,否则在编译阶段就会报错
|
||||
//!
|
||||
//! 因为使用未初始化的变量通常会导致无法预知的行为,所以不允许使用未初始化的变量
|
||||
//!
|
||||
fn main() {
|
||||
// 声明一个变量 但不对其进行初始化
|
||||
let a_binding;
|
||||
|
||||
{
|
||||
let x = 2;
|
||||
|
||||
// 初始化变量值
|
||||
a_binding = x * x;
|
||||
}
|
||||
|
||||
println!("a binding: {}", a_binding);
|
||||
|
||||
let another_binding;
|
||||
|
||||
// 报错! 使用了未初始化的变量
|
||||
println!("another binding: {}", another_binding);
|
||||
// FIXME ^ 注释这行代码
|
||||
|
||||
another_binding = 1;
|
||||
|
||||
println!("another binding: {}", another_binding);
|
||||
}
|
||||
7
4.Variable Bindings/4.4 Freezing/Cargo.lock
generated
Normal file
7
4.Variable Bindings/4.4 Freezing/Cargo.lock
generated
Normal file
@@ -0,0 +1,7 @@
|
||||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "freezing"
|
||||
version = "0.1.0"
|
||||
8
4.Variable Bindings/4.4 Freezing/Cargo.toml
Normal file
8
4.Variable Bindings/4.4 Freezing/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "freezing"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
22
4.Variable Bindings/4.4 Freezing/src/main.rs
Normal file
22
4.Variable Bindings/4.4 Freezing/src/main.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
//!
|
||||
//! 可以通过使用子作用域遮蔽(shadowing)父作用域的同名可变变量成不可变变量
|
||||
//! 这样在这个子作用域内的同名变量就会是 冻结(Freezing)状态
|
||||
//! 直到子作用域结束
|
||||
//!
|
||||
fn main() {
|
||||
let mut _mutable_integer = 7i32;
|
||||
|
||||
{
|
||||
// 使用不可变遮蔽父作用域的同名变量
|
||||
let _mutable_integer = _mutable_integer;
|
||||
|
||||
// 报错! 当前作用域中 `_mutable_integer` 变量是不可变的
|
||||
_mutable_integer = 50;
|
||||
// FIXME ^ 注释这行代码
|
||||
|
||||
// `_mutable_integer` 变量作用域结束
|
||||
}
|
||||
|
||||
// `_mutable_integer` 这里可以修改,因为父作用域的定义是可变的
|
||||
_mutable_integer = 3;
|
||||
}
|
||||
Reference in New Issue
Block a user