init code
This commit is contained in:
7
2.Primitives/2.1 Literals and operators/Cargo.lock
generated
Normal file
7
2.Primitives/2.1 Literals and operators/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 = "literals_and_operators"
|
||||
version = "0.1.0"
|
||||
8
2.Primitives/2.1 Literals and operators/Cargo.toml
Normal file
8
2.Primitives/2.1 Literals and operators/Cargo.toml
Normal file
@@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "literals_and_operators"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
44
2.Primitives/2.1 Literals and operators/src/main.rs
Normal file
44
2.Primitives/2.1 Literals and operators/src/main.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
fn main() {
|
||||
// 无符号整数加法
|
||||
println!("1 + 2 = {}", 1u32 + 2);
|
||||
|
||||
// 有符号整数减法
|
||||
println!("1 - 2 = {}", 1i32 - 2);
|
||||
// TODO ^ 修改 `1i32` 成 `1u32` 会编译失败,因为无符号不能表示负数
|
||||
|
||||
// 科学计数法
|
||||
println!("1e4 is {}, -2.5e-3 is {}", 1e4, -2.5e-3);
|
||||
|
||||
// 逻辑表达式语法糖
|
||||
println!("true AND false is {}", true && false);
|
||||
println!("true OR false is {}", true || false);
|
||||
println!("NOT true is {}", !true);
|
||||
|
||||
// 二进制操作
|
||||
|
||||
// 二进制 位与
|
||||
// 两个数字二进制的 相同位 都是 1 的时候,结果的对应位置上为 1 否则为 0
|
||||
println!("0011 AND 0101 is {:04b}", 0b0011u32 & 0b0101);
|
||||
|
||||
// 二进制 位或
|
||||
// 两个数字二进制的 相同位 任意一个 1 的时候,结果的对应位置上为 1 否则为 0
|
||||
println!("0011 OR 0101 is {:04b}", 0b0011u32 | 0b0101);
|
||||
|
||||
// 二进制 位异或
|
||||
// 两个数字二进制的 相同位 不相同的时候,结果的对应位置上为 1 否则为 0
|
||||
println!("0011 XOR 0101 is {:04b}", 0b0011u32 ^ 0b0101);
|
||||
|
||||
// 二进制 左移
|
||||
// 数字的二进制位 整体向左移动指定位数 新增的位置补 0
|
||||
// 0b1 << 5 == 0b100000 == 32
|
||||
println!("1 << 5 is {}", 1u32 << 5);
|
||||
|
||||
// 二进制 右移
|
||||
// 数字的二进制位 整体向右移动指定位数 移动过程中会丢弃末尾的二进制位
|
||||
// 0b10000000 >> 2 == 0b100000 == 32 == 0x20
|
||||
println!("0x80 >> 2 is 0x{:x}", 0x80u32 >> 2);
|
||||
|
||||
// 可以通过在数字中插入 `_` 来让数字读起来更容易一些
|
||||
// 改特性不会影响数字本身,编译的过程中会自动去掉数字中的 `_`
|
||||
println!("One million is written as {}", 1_000_000u32);
|
||||
}
|
||||
Reference in New Issue
Block a user