#[derive(Debug)] 做了什么
- 加上#[derive(Debug)]属性时,编译器 会自动为这个类型生成 Debug特征的实现代码。
-
- 就可以使用 println!("{:?}", your_variable) 或者 dbg!(your_variable) 这样的语句来打印出该变量的详细信息了。
rust
#[derive(Debug)]
struct Point {
x: i32,
y: i32,
}
fn main() {
let p = Point { x: 10, y: 20 };
println!("{:?}", p);
}
rust
#[derive(Debug)]
struct Rectangle {
width: u32,
length: u32,
}
// 结构体 实现方法
impl Rectangle {
// 传入引用的 结构体
fn area(&self) -> u32 {
self.width * self.length
}
fn can_hold(&self, other: &Rectangle) -> bool {
self.width >= other.width && self.length >= other.length
}
}
fn main() {
let rect1: Rectangle = Rectangle {
width: 30,
length: 50,
};
let rect2: Rectangle = Rectangle {
width: 10,
length: 50,
};
let rect3: Rectangle = Rectangle {
width: 40,
length: 50,
};
println!("{}", rect1.can_hold(&rect2));
println!("{}", rect1.can_hold(&rect3));
println!("{}", rect1.area());
println!("{:#?}", rect1);
}
rust
#[derive(Debug)]
struct Rectangle {
width: u32,
length: u32,
}
// 结构体 实现方法
impl Rectangle {
// 传入引用的 结构体
fn area(&self) -> u32 {
self.width * self.length
}
fn can_hold(&self, other: &Rectangle) -> bool {
self.width >= other.width && self.length >= other.length
}
// 关联函数
fn square(size: u32) -> Rectangle {
Rectangle {
width: size,
length: size,
}
}
}
fn main() {
let rect1: Rectangle = Rectangle {
width: 30,
length: 50,
};
let rect2: Rectangle = Rectangle {
width: 10,
length: 50,
};
let rect3: Rectangle = Rectangle {
width: 40,
length: 50,
};
// 调用关联方法
let s1: Rectangle = Rectangle::square(32);
println!("{}", rect1.can_hold(&rect2));
println!("{}", rect1.can_hold(&rect3));
println!("{}", rect1.area());
println!("{:#?}", rect1);
println!("{:#?}", s1);
}
println!("{:#?}", s1);
这个看里面几个特殊字符有什么意义
- {}这是格式化输出的占位符
- #当与格式化输出的 {} 结合使用{:#?} 这种形式相比于 {:?} 会让输出的格式更 "美观" 、易读,呈现出更有层次结构的缩进格式,便于查看复杂数据结构内部详细的成员信息等
- ? 在格式化输出的上下文中,与 {} 配合使用时用于告诉 Rust 编译器按照实现的 Debug 特性的要求来格式化输出对应的值。
rust
enum IpAddrKind {
v4(u8, u8, u8, u8),
v6(String),
}
fn main() {
let home = IpAddrKind::v4(127, 0, 0, 1);
let loopback = IpAddrKind::v6(String::from("::1"));
}
rust
#[derive(Debug)]
enum IpAddrKind {
v4(u8, u8, u8, u8),
v6(String),
}
impl IpAddrKind {
fn ping(&self) {
println!("{:?}", self)
}
}
fn main() {
let home = IpAddrKind::v4(127, 0, 0, 1);
let loopback = IpAddrKind::v6(String::from("::1"));
home.ping();
}
rust
fn main() {
let some_number = Some(5);
let some_string = Some("slice str");
let absent_number: Option<i32> = None;
}
rust
enum Coin {
Penny,
Nickel,
Dime,
Quarter,
}
fn value_in_cents(coin: Coin) -> u32 {
match coin {
Coin::Penny => 1,
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter => 25,
}
}
fn main() {
value_in_cents(Coin::Dime);
}
rust
#[derive(Debug)]
enum Coin {
Penny,
Nickel,
Dime,
Quarter,
}
fn value_in_cents(coin: Coin) -> u32 {
match coin {
Coin::Penny => 1,
Coin::Nickel => 5,
Coin::Dime => {
println!("{:#?}", coin);
1
}
Coin::Quarter => 25,
}
}
fn main() {
value_in_cents(Coin::Dime);
}
rust
#[derive(Debug)]
enum UsState {
Alabama,
Alaska,
Arizona,
Arkansas,
California,
Colorado,
Connecticut,
Delaware,
Florida,
Georgia,
Hawaii,
Idaho,
Illinois,
Indiana,
Iowa,
Kansas,
Kentucky,
Louisiana,
Maine,
Maryland,
Massachusetts,
Michigan,
Minnesota,
Mississippi,
Missouri,
Montana,
Nebraska,
Nevada,
NewHampshire,
NewJersey,
NewMexico,
NewYork,
NorthCarolina,
NorthDakota,
Ohio,
Oklahoma,
Oregon,
Pennsylvania,
RhodeIsland,
SouthCarolina,
SouthDakota,
Tennessee,
Texas,
Utah,
Vermont,
Virginia,
Washington,
WestVirginia,
Wisconsin,
Wyoming,
}
enum Coin {
Penny,
Nickel,
Dime,
Quarter(UsState),
}
fn value_in_cents(coin: Coin) -> u32 {
match coin {
Coin::Dime => 1,
Coin::Nickel => 5,
Coin::Penny => 10,
Coin::Quarter(state) => {
println!("{:#?}", state);
25
}
}
}
fn main() {
value_in_cents(Coin::Quarter(UsState::Alabama));
}