Rust编程初探:深入理解Struct结构体一、定义并实例化struct什么是structstruct,结构体自定义的数据类型为相关联的值命名,打包=>有意义的组合定义struct使用struct关键字,并为整个struct命名在花括号内,为所有字段(Fiel
struct
关键字,并为整个struct命名例如:
struct User {
username: String,
email: String,
sign_in_count: u64,
active: bool,
}
例子:
let user1 = User {
email: String::from("someone@example.com"),
username: String::from("someusername123"),
active: true,
sign_in_count: 1,
};
代码:
struct User {
username: String,
email: String,
sign_in_count: u64,
active: bool,
}
fn main() {
println!("Hello, world!");
let user1 = User {
email: String::from("user1@example.com"),
username: String::from("user1"),
active: true,
sign_in_count: 556,
};
}
let mut user1 = User {
email: String::from("someone@example.com"),
username: String::from("someusername123"),
active: true,
sign_in_count: 1,
};
user1.email = String::from("anotheremail@example.com");
例子:
fn build_user(email: String, username: String) -> User {
User {
email: email,
username: username,
active: true,
sign_in_count: 1,
}
}
fn build_user(email: String, username: String) -> User {
User {
email,
username,
active: true,
sign_in_count: 1,
}
}
let user2 = User {
email: String::from("another@example.com"),
username: String::from("anotherusername567"),
active: user1.active,
sign_in_count: user1.sign_in_count,
};
let user2 = User {
email: String::from("another@example.com"),
username: String::from("anotherusername567"),
..user1
};
struct Color(i32, i32, i32);
struct Point(i32, i32, i32);
let black = Color(0, 0, 0);
let origin = Point(0, 0, 0);
struct User {
username: String,
email: String,
sign_in_count: u64,
active: bool,
}
// 没有使用生命周期
struct User {
username: &str,
email: &str,
sign_in_count: u64,
active: bool,
}
fn main() {
println!("Hello, world!");
let user1 = User {
email: "fdsa",
username: "fds",
active: true,
sign_in _count: 556,
};
}
例子需求
fn main() {
let w = 30;
let l = 50;
println!("{}", area(w, l));
}
fn area(width: u32, length: u32) -> u32 {
width * length
}
优化一:
fn main() {
let rect = (30, 50);
println!("{}", area(rect));
}
fn area(dim: (u32, u32)) -> u32 {
dim.0 * dim.1
}
优化二:
struct Rectangle {
width: u32,
length: u32,
}
fn main() {
let rect = Rectangle {
width: 30,
length: 50,
};
println!("{}", area(&rect));
}
fn area(rect: &Rectangle) -> u32 {
rect.width * rect.length
}
std::fmt::Display
std::fmt::Debug
#[derive(debug)]
{:?}
{:#?}
#[derive(Debug)]
struct Rectangle {
width: u32,
length: u32,
}
fn main() {
let rect = Rectangle {
width: 30,
length: 50,
};
println!("{}", area(&rect));
println!("{:?}", rect);
println!("{:#?}", rect);
}
fn area(rect: &Rectangle) -> u32 {
rect.width * rect.length
}
#[derive(Debug)]
struct Rectangle {
width: u32,
length: u32,
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.length
}
}
fn main() {
let rect = Rectangle {
width: 30,
length: 50,
};
println!("{}", rect.area());
println!("{:?}", rect);
println!("{:#?}", rect);
}
ogject->something()
和 (*object).something()
一样p1.distance(&p2);
(&p1).distance(&p2);
#[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 rect = Rectangle {
width: 30,
length: 50,
};
let rect1 = Rectangle {
width: 30,
length: 50,
};
let rect2 = Rectangle {
width: 10,
length: 40,
};
let rect3 = Rectangle {
width: 35,
length: 55,
};
println!("{}", rect1.can_hold(&rect2));
println!("{}", rect1.can_hold(&rect3));
println!("{}", rect.area());
println!("{:?}", rect);
println!("{:#?}", rect);
}
// fn area(rect: &Rectangle) -> u32 {
// rect.width * rect.length
// }
String::from()
#[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 s = Rectangle::square(20);
let rect = Rectangle {
width: 30,
length: 50,
};
let rect1 = Rectangle {
width: 30,
length: 50,
};
let rect2 = Rectangle {
width: 10,
length: 40,
};
let rect3 = Rectangle {
width: 35,
length: 55,
};
println!("{}", rect1.can_hold(&rect2));
println!("{}", rect1.can_hold(&rect3));
println!("{}", rect.area());
println!("{:?}", rect);
println!("{:#?}", rect);
println!("{:?}", s)
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!