struct 或者 structure ,是一个自定数据类型,允许你 包装 和 命名 多个相关的值,从而形成一个有意义的组合struct User{
active: bool,
username: String,
email: String,
sign_in_count: u64,
}
fn main(){
let mut userOne = User {
active: true,
username: String::from("张三"),
email: String::from("ghekswew@gmail.com"),
sign_in_count: 1,
};
println!("The userOne is email: {}",userOne.email);//ghekswew@gmail.com
// 结构体的实例是可变的,可以使用点号并为对应的字段赋值,以达到修改的目的
userOne.email = String::from("jkfhfh@gmail.com");
}
fn build_user(email: String,username: String) -> User{
User{
active: true,
username: username,
email: email,
sign_in_count: 1,
}
}
email和用户名的User实例参数名和字段名都完全相同,使用字段初始化简写语法//重写build_user方法
fn build_user(email: String,username: String)->User{
User{
active: true,
username,
email,
sign_in_count: 1,
}
}
let user2 = User {
active: user1.active,
username: user1.username,
email: String::from("ghescjsjs@gmail.com"),
sign_in_count: user1.sign_in_count,
};
..语法指定了剩余未显示设置值的字段应有与给定实例对应字段相同的值let user3 = User {
email: String::from("hsxhsgwh@gmail.com"),
// 这里不能使用user1,因为user1的username字段中的String被移动到user2,user1的username中的String失效`在这里插入代码片`
..user2
};
struct Color(i32,i32,i32);
struct Point(i32,i32,i32);
fn main(){
let black = Color(0,0,0);
let origin = Point(0,0,0)
}
. +索引访问值等等struct AlwaysEqual;
fn main(){
let subject = AlwaysEqual;
}
fn main() {
let width = 30;
let height = 50;
println!(
"The area of the rectangle is {} square pixels.",
area(width, height)
);
}
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)
);
}
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)
);
}
fn area(rectangle: &Rectangle) -> u32 {
rectangle.width * rectangle.height
}
#[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 }
/*
rect1 is Rectangle {
width: 30,
height: 50,
}
*/
//println!("rect1 is {:#?}", rect1);
/*
[src\main.rs:22] &rect1 = Rectangle {
width: 30,
height: 50,
}
*/
dbg!(&rect1);
}
dbg! 宏接收一个表达式的所有权(与 println! 宏想法,后者接收的是引用)dbg!宏时所在文件和行号,以及该表达式的结果值,并返回该值的所有权fn关键字 和 名称声明,可以拥有 参数 和 返回值 ,同时包含在某处调用该方法时会执行的代码第一个参数总是self,它代表调用该方法的结构体实例#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
}
fn main() {
let rect1 = Rectangle {
width: 30,
height: 50,
};
println!(
"The area of the rectangle is {} square pixels.",
rect1.area()
);
}
Rectangle的上下文中,使用impl块中的所有内容都将与Rectangle类型相关联area 方法area的签名中,使用&self来替代rectangle: &Rectangle,&self实际上是self: &self的缩写==========================================================
getters#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
fn width(&self) -> bool {
self.width > 0
}
}
fn main() {
let rect1 = Rectangle {
width: 30,
height: 50,
};
println!(
"The area of the rectangle is {} square pixels.",
rect1.area()
);
println!("The width is gt 0:{}", rect1.width()); //true
}
. 直接在对象上调用方法先解引用
object -> something() 和 (*object).something()一样->等效的运算符,相反,Rust有自动引用和解引用的功能p1.distance(&p2)和(&p1).distance(&p2)#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
fn width(&self) -> bool {
self.width > 0
}
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)); //true
println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3)); // false
println!(
"The area of the rectangle is {} square pixels.",
rect1.area()
);
println!("The width is gt 0:{}", rect1.width()); //true
}
self为第一参数的关联函数(不是方法),它们并不作用于一个结构体的实例结构体命名和::语法来调用这个关联函数#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn square(size: u32) -> Self {
Self {
width: size,
height: size,
}
}
}
fn main() {
let sq = Rectangle::square(3);
println!("{}", sq.width); //3
}
impl块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
}
}