结构体
https://rustwiki.org/zh-CN/book/ch05-00-structs.html
基础的定义与赋值
// 定义结构体
struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
}
fn main() {
// 生成结构体
// 注意整个实例必须是可变的;Rust 并不允许只将某个字段标记为可变
let mut user1 = User {
email: String::from("[email protected]"),
username: String::from("someusername123"),
active: true,
sign_in_count: 1,
};
// 改变结构体中的值
user1.email = String::from("[email protected]");
}基础的定义与赋值 之 结构体应拥有数据的所有权
struct User {
active: bool,
username: &str, // wrong! use String!
email: &str, // wrong! use String!
sign_in_count: u64,
}
fn main() {
let user1 = User {
email: "[email protected]",
username: "someusername123",
active: true,
sign_in_count: 1,
};
}
语法糖:变量与字段同名
struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
}
fn build_user(email: String, username: String) -> User {
User {
email, // email:email,
username, // username:username,
active: true,
sign_in_count: 1,
}
}
fn main() {
let user1 = build_user(
String::from("[email protected]"),
String::from("someusername123"),
);
}
语法糖:从别的结构体为模板创建新结构体
struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
}
fn main() {
let user1 = User {
email: String::from("[email protected]"),
username: String::from("someusername123"),
active: true,
sign_in_count: 1,
};
// let user2 = User {
// active: user1.active,
// username: user1.username,
// email: String::from("[email protected]"),
// sign_in_count: user1.sign_in_count,
// };
let user2 = User {
email: String::from("[email protected]"),
..user1
};
}元组结构体
struct Color(i32, i32, i32);
struct Point(i32, i32, i32);
fn main() {
let black = Color(0, 0, 0);
let origin = Point(0, 0, 0);
}需求 1:我们打算做的是,打印矩形面积。
分别定义长度与宽度
fn main() {
let width1 = 30;
let height1 = 50;
println!(
"The area of the rectangle is {} square pixels.",
area(width1, height1)
);
// The area of the rectangle is 1500 square pixels.
}
fn area(width: u32, height: u32) -> u32 {
width * height
}采用元组定义矩形
fn main() {
let rect1 = (30, 50);
println!(
"The area of the rectangle is {} square pixels.",
area(rect1)
);
// The area of the rectangle is 1500 square pixels.
}
fn area(dimensions: (u32, u32)) -> u32 {
dimensions.0 * dimensions.1
}采用结构体定义矩形
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let rect1 = Rectangle {
width: 30,
height: 50,
};
println!(
"The area of the rectangle is {} square pixels.",
area(&rect1)
);
// The area of the rectangle is 1500 square pixels.
}
fn area(rectangle: &Rectangle) -> u32 {
rectangle.width * rectangle.height
}需求 2:直接打印数据集结构的内容
使用trait Debug,并通过 println! 宏打印
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let rect1 = Rectangle {
width: 30,
height: 50,
};
println!("rect1 is {:?}", rect1);
// rect1 is Rectangle { width: 30, height: 50 },
println!("rect1 is {:#?}", rect1);
// rect1 is Rectangle {
// width: 30,
// height: 50,
// }
}使用trait Debug,并通过 dbg! 宏打印
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let scale = 2;
let rect1 = Rectangle {
width: dbg!(30 * scale),
height: 50,
};
dbg!(&rect1); // 我们不希望 dbg! 拥有 rect1 的所有权,所以我们在下一次调用 dbg! 时传入一个引用
}
// [src/main.rs:10:16] 30 * scale = 60
// [src/main.rs:14:5] &rect1 = Rectangle {
// width: 60,
// height: 50,
// }
方法
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 { // 注意要有 self
self.width * self.height
}
}
fn main() {
let rect1 = Rectangle {
width: 30,
height: 50,
};
println!(
"The area of the rectangle is {} square pixels.",
rect1.area()
);
}
带返回值的方法
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn width(&self) -> bool {
self.width > 0
}
}
fn main() {
let rect1 = Rectangle {
width: 30,
height: 50,
};
if rect1.width() {
println!("The rectangle has a nonzero width; it is {}", rect1.width);
}
}带传入参数的方法
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
fn can_hold(&self, other: &Rectangle) -> bool {
self.width > other.width && self.height > other.height
}
}
fn main() {
let rect1 = Rectangle {
width: 30,
height: 50,
};
let rect2 = Rectangle {
width: 10,
height: 40,
};
let rect3 = Rectangle {
width: 60,
height: 45,
};
println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2));
println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3));
}可以为一个 struct 创建 多个impl
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
}
impl Rectangle {
fn can_hold(&self, other: &Rectangle) -> bool {
self.width > other.width && self.height > other.height
}
}
fn main() {
let rect1 = Rectangle {
width: 30,
height: 50,
};
let rect2 = Rectangle {
width: 10,
height: 40,
};
let rect3 = Rectangle {
width: 60,
height: 45,
};
println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2));
println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3));
}最后更新于
这有帮助吗?