Skip to content

Commit ed0cffd

Browse files
committed
examples: debug
1 parent 9da6ea7 commit ed0cffd

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed

examples/debug/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/debug/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "debug"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

examples/debug/src/main.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
fn main() {
2+
print();
3+
dbg();
4+
}
5+
6+
// `derive` 属性自动创建实现
7+
// 需要使用 `fmt::Debug` 使这个 `struct` 可打印
8+
#[derive(Debug)]
9+
#[allow(dead_code)] // 消除代码未被使用的警告信息
10+
struct User {
11+
name: String,
12+
age: i32,
13+
email: Option<String>, // 可选参数,如果没有则为 None
14+
}
15+
16+
fn print() {
17+
println!("\n*********** 无参数 ***********\n");
18+
// output: Hello, world!
19+
println!("Hello, world!");
20+
21+
println!("\n*********** 多个参数 ***********\n");
22+
// output: hello rust
23+
println!("{} {}", "hello", "rust");
24+
25+
println!("\n*********** 参数重排序 ***********\n");
26+
// 使用参数索引重新组合排序,支持键值对
27+
// ⚠️ 注意:属性参数位置必须在索引参数之后,否则会报错:positional arguments cannot follow named arguments
28+
// output: "lencx": 1 2 rust 3
29+
println!("{name:?}: {0:?} {1:?} {lang} {2:?} ",
30+
1, // 1:?
31+
2, // 2:?
32+
3, // 3:?
33+
lang="rust", // lang - 字符串格式输出,不带双引号
34+
name="lencx", // name:? - 字符串格式输出,带双引号
35+
);
36+
37+
println!("\n*********** Struct ***********\n");
38+
// output: User { name: "lencx", age: 18, email: None }
39+
let user = User { name: "lencx".into(), age: 18, email: None };
40+
println!("{:?}", user);
41+
42+
println!("\n----- Struct 格式美化 -----\n");
43+
// output:
44+
// User {
45+
// name: "lencx",
46+
// age: 18,
47+
// email: None,
48+
// }
49+
println!("{:#?}", user);
50+
51+
println!("\n*********** 进制转换 ***********\n");
52+
println!("进制 10: {}", 69420); // 69420
53+
println!("进制 2 (binary): {:b}", 69420); // 10000111100101100
54+
println!("进制 8 (octal): {:o}", 69420); // 207454
55+
println!("进制 16 (hexadecimal): {:x}", 69420); // 10f2c
56+
println!("进制 16 (hexadecimal): {:X}", 69420); // 10F2C
57+
58+
println!("\n*********** 补齐 ***********\n");
59+
println!("\n----- 补空字符串 -----\n");
60+
// 右对齐:字符左边补充 15 个空字符
61+
// output: " hello"
62+
println!("{0:>15}", "hello");
63+
// 右对齐:字符左边补充 10 个空字符
64+
// output: "Name: lencx"
65+
println!("Name:{name:>10}", name="lencx");
66+
67+
println!("\n----- 数字补零 -----\n");
68+
// 用额外的零填充在数字后面
69+
println!("{number:0<5}", number=1); // 10000
70+
// 用额外的零填充在数字前面
71+
println!("{number:0>5}", number=1); // 00001
72+
// width: 通过附加“$”在格式说明符中使用命名参数,需要在数字前补零的个数
73+
println!("{number:0>width$}", number=5, width=10); // 0000000005
74+
75+
println!("\n----- 周围变量捕获参数 -----\n");
76+
// 对于 Rust 1.58 及更高版本,您可以直接从周围变量中捕获参数,a 和 b 为变量
77+
let a: f64 = 1.0;
78+
let b: usize = 5;
79+
println!("{a:0>b$}"); // 补零:00001
80+
println!("{a:>b$}"); // 补空: " 1"
81+
}
82+
83+
fn dbg() {
84+
// [src/main.rs:90] user = User {
85+
// name: "lencx",
86+
// age: 18,
87+
// email: None,
88+
// }
89+
let user = User { name: "lencx".into(), age: 18, email: None };
90+
dbg!(user);
91+
92+
// [src/main.rs:95] "a" = "a"
93+
// [src/main.rs:95] "b" = "b"
94+
// [src/main.rs:95] 1 = 1
95+
dbg!("a", "b", 1);
96+
}

0 commit comments

Comments
 (0)