"首次提交"
This commit is contained in:
1
struct_learn/.gitignore
vendored
Executable file
1
struct_learn/.gitignore
vendored
Executable file
@ -0,0 +1 @@
|
||||
/target
|
8
struct_learn/Cargo.toml
Executable file
8
struct_learn/Cargo.toml
Executable file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "struct_learn"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
170
struct_learn/src/main.rs
Executable file
170
struct_learn/src/main.rs
Executable file
@ -0,0 +1,170 @@
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
struct01();
|
||||
let user = build_user(String::from("qwer"), String::from("ertyfd"));
|
||||
println!("user info name:{}, age:{}, email:{}", user.name, user.age, user.email);
|
||||
let user1 = ref_build_use1(user);
|
||||
println!("user1 info name:{}, age:{}, email:{}", user1.name, user1.age, user1.email);
|
||||
|
||||
sturct_empty();
|
||||
|
||||
tuple_struct();
|
||||
struct_method_test();
|
||||
}
|
||||
|
||||
|
||||
fn struct01()
|
||||
{
|
||||
//定义一个结构体类型,使用struct为关键字,后面跟其名字,
|
||||
//定义成员,类型后置,每个成员定义后都需要使用逗号分隔
|
||||
struct User{
|
||||
name:String, //类型后置,逗号分隔
|
||||
email:String,
|
||||
age:i32,
|
||||
is_man:bool,
|
||||
sign_cnt:i64, //最后一个成员也需要加逗号
|
||||
} //无需使用分号
|
||||
|
||||
// 使用指定的类型初始化一个实例,每个成员都必须进行初始化赋值。
|
||||
let mut user = User{
|
||||
name:String::from("ekko.bao"),
|
||||
email:String::from("yucang_bao@126.com"),
|
||||
age:8,
|
||||
is_man:true,
|
||||
sign_cnt:2222222,
|
||||
};
|
||||
//访问成员使用点分隔符进行访问
|
||||
println!("user info name:{}, age:{}, email:{}", user.name, user.age, user.email);
|
||||
//struct所有成员mut是整体的不可单个设置,要么所有都可被修改要么都不可被修改。
|
||||
user.age = 888;
|
||||
}
|
||||
|
||||
struct User{
|
||||
name:String, //类型后置,逗号分隔
|
||||
email:String,
|
||||
age:i32,
|
||||
is_man:bool,
|
||||
sign_cnt:i64, //最后一个成员也需要加逗号
|
||||
}
|
||||
|
||||
fn build_user(name:String, email:String) -> User {
|
||||
// 这里展示了一种初始化struct的简便方法:
|
||||
User{
|
||||
name, //等效于 name:name, 当想要初始化的成员名和变量名一致时可直接省略赋值操作
|
||||
email,//等效于 name:email
|
||||
age:10,
|
||||
is_man:false,
|
||||
sign_cnt:66666,
|
||||
}
|
||||
}
|
||||
|
||||
fn ref_build_use1(user1:User) -> User {
|
||||
// 这里展示了一种初始化struct的简便方法:
|
||||
// 当我们一部分内容想要自定义初始化一部分来自另外一个stuct,
|
||||
// 那么我们可以使用如下的语法进行
|
||||
User{
|
||||
/* 填写自定义初始化的内容 */
|
||||
name:String::from("user1.bao"),
|
||||
email:String::from("zzzzzzz@126.com"),
|
||||
age:77,
|
||||
/* 选择继承已有结构体,未初始化的其他成员将会使用该结构体进行初始化
|
||||
该结构体必须是和当前想要初始化的结构体一样的类型才可以
|
||||
*/
|
||||
..user1
|
||||
}
|
||||
}
|
||||
|
||||
fn sturct_empty(){
|
||||
// stuct可以是空成员的,这个是为了只实现一些方法但是不提供成员数据的应用场景。
|
||||
struct empty_struct{
|
||||
|
||||
}
|
||||
|
||||
let test = empty_struct{};
|
||||
}
|
||||
|
||||
fn tuple_struct(){
|
||||
// tuple struct 是类似元组的struct,没有成员名只有类型。
|
||||
// 使用括号进行包裹所有成员
|
||||
struct RGB(u8,u8,u8);
|
||||
let color = RGB(34,233,123);
|
||||
println!("color is R:{} G:{} B:{}", color.0, color.1, color.2);
|
||||
|
||||
struct POINT(u8,u8,u8);
|
||||
let point = POINT(34,233,123);
|
||||
println!("point is X:{} Y:{} Z:{}", point.0, point.1, point.2);
|
||||
|
||||
// let point:POINT = color; // color和point虽然成员的类型和个数都是一样的,但是是不同的类型!,所以不可以进行绑定
|
||||
let color1 = RGB{0:23,..color};//同样可以使用这种继承部分成员的赋值方法
|
||||
println!("color1 is R:{} G:{} B:{}", color1.0, color1.1, color1.2);
|
||||
|
||||
let color2 = color1; //color1 所有权移交给color2 color1不可在使用
|
||||
println!("color2 is R:{} G:{} B:{}", color2.0, color2.1, color2.2);
|
||||
//println!("color1 is R:{} G:{} B:{}", color1.0, color1.1, color1.2);//不可使用
|
||||
}
|
||||
|
||||
|
||||
//注解 ,可以打印结构体内容
|
||||
#[derive(Debug)]
|
||||
struct Rectangle {
|
||||
width: i32,
|
||||
height: i32,
|
||||
}
|
||||
|
||||
//定义结构体Rectangle的实现
|
||||
impl Rectangle{
|
||||
/* 第一个参数为self的是该结构体的方法, 就类似于类的成员函数 */
|
||||
//求长方形面积
|
||||
fn area (&self) -> i32{ // 使用引用的方式使用self, 可以不显式的标注类型
|
||||
//fn area (self: &Rectangle) -> i32{ // 使用类型后置标注的方式也是ok的
|
||||
//fn area (self: & mut Rectangle) -> i32{ // 也可以接上mut关键字, 这使得可以修改成员变量
|
||||
// self.width = self.height;
|
||||
//fn area (self: Rectangle) -> i32{ // 非引用的方式使用也是ok的,但是要注意所有权的问题
|
||||
self.width * self.height
|
||||
}
|
||||
/* 第一个参数不是self的是该结构体的关联函数 */
|
||||
fn new(width: i32, height: i32) -> Rectangle{
|
||||
Rectangle{
|
||||
width,
|
||||
height,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//impl可以分布在多个地方。只需要进行impl包裹即可
|
||||
impl Rectangle{
|
||||
//根据传入的矩形创建一个外接的正方形
|
||||
fn create_hold_square(other:&Rectangle) -> Rectangle{
|
||||
let width = if other.width > other.height {other.width} else {other.height};
|
||||
Rectangle{
|
||||
width,
|
||||
height: width,
|
||||
}
|
||||
}
|
||||
|
||||
//检查一个矩形是否可以完全按包住另一个矩形
|
||||
//像这样类型后置的申明self的类型的方式也是可以的
|
||||
fn can_hold(self: &Rectangle, other:&Rectangle ) -> bool{
|
||||
if self.width >= other.width && self.height >= other.height {
|
||||
true
|
||||
}
|
||||
else{
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn struct_method_test(){
|
||||
//使用关联函数初始化一个矩形
|
||||
let rect = Rectangle::new(30,30);
|
||||
println!("rect is{:#?}, area is {}", rect, rect.area());
|
||||
|
||||
let rect = Rectangle{
|
||||
width: 60,
|
||||
height: 40,
|
||||
};
|
||||
|
||||
let rect1 = Rectangle::create_hold_square(&rect);
|
||||
println!("rect is {:?}, rect1 is {:?}", rect, rect1);
|
||||
println!("rect1 {} hold rect", if rect1.can_hold(&rect) {"can"} else {"can not"});
|
||||
}
|
Reference in New Issue
Block a user