数组用于存储相同类型值的集合,类似地,结构Struct是Rust用于自定义数据类型,它使我们能够组合不同类型的数据项。
struct 关键字用于声明结构,由于结构是静态类型的,因此结构中的每个字段都必须与数据类型相关联。
struct Name_of_structure { field1:data_type, field2:data_type, field3:data_type }
声明结构后,应为每个字段分配一个值,这称为初始化。
let instance_name=Name_of_structure { field1:value1, field2:value2, field3:value3 }; struct Employee { name:String, company:String, age:u32 } fn main() { let emp1=Employee { company:String::from("LearnFKPoint"), name:String::from("Guanxi"), age:50 }; println!("Name is :{} company is {} age is {}",emp1.name,emp1.company,emp1.age); }
上面的示例声明一个具有三个字段的结构Employee:name,company和age,main()初始化结构,它使用println!宏以打印结构中定义的字段的值。
Name is :Guanxi company is LearnFKPoint age is 50
要修改结构,应将变量标签为可变的,下面的示例声明并初始化名为 Employee 的结构,然后将 age 字段的值从50修改为40。
let mut emp1=Employee { company:String::from("LearnFKPoint"), name:String::from("Guanxi"), age:50 }; emp1.age=40; println!("Name is :{} company is {} age is {}",emp1.name,emp1.company,emp1.age);
Name is :Guanxi company is LearnFKPoint age is 40
以下示例显示如何将struct作为参数传递, display方法将Employee作为参数并打印详细信息。
fn display( emp:Employee) { println!("Name is :{} company is {} age is {}",emp.name,emp.company,emp.age); }
这是完整的程序-
//声明一个结构 struct Employee { name:String, company:String, age:u32 } fn main() { // 初始化结构 let emp1=Employee { company:String::from("LearnFKPoint"), name:String::from("Guanxi"), age:50 }; let emp2=Employee{ company:String::from("LearnFKPoint"), name:String::from("Guandong"), age:32 }; //将 emp1 和 emp2 传递给 display() display(emp1); display(emp2); } //将数据打印到控制台 fn display( emp:Employee){ println!("Name is :{} company is {} age is {}",emp.name,emp.company,emp.age); }
Name is :Guanxi company is LearnFKPoint age is 50 Name is :Guandong company is LearnFKPoint age is 32
让我们考虑一个函数 who_is_elder(),该函数比较两名员工的年龄并返回其中一位。
fn who_is_elder (emp1:Employee,emp2:Employee)->Employee { if emp1.age>emp2.age { return emp1; } else { return emp2; } }
这是完整的程序-
fn main() { //初始化结构 let emp1=Employee{ company:String::from("LearnFKPoint"), name:String::from("Guanxi"), age:50 }; let emp2=Employee { company:String::from("LearnFKPoint"), name:String::from("Guandong"), age:32 }; let elder=who_is_elder(emp1,emp2); println!("elder is:"); //打印年长员工的详细信息 display(elder); } //接受员工结构的实例并比较他们的年龄 fn who_is_elder (emp1:Employee,emp2:Employee)->Employee { if emp1.age>emp2.age { return emp1; } else { return emp2; } } //显示员工的姓名、公司和年龄 fn display( emp:Employee) { println!("Name is :{} company is {} age is {}",emp.name,emp.company,emp.age); } //声明一个结构 struct Employee { name:String, company:String, age:u32 }
elder is: Name is :Guanxi company is LearnFKPoint age is 50
方法就像函数,它们是编程指令的逻辑组,方法用 fn 关键字声明,方法的范围在结构块内,方法的第一个参数将始终为 self ,它表示结构的调用。
struct My_struct {} impl My_struct { //设置方法的上下文 fn method_name() { //定义一个方法 } }
以下示例定义一个结构矩形,其字段为- width 和 height ,方法 area 在结构中定义, area方法通过 self 关键字访问结构的字段,并计算矩形的面积。
//定义矩形的尺寸 struct Rectangle { width:u32, height:u32 } //计算矩形面积的逻辑 impl Rectangle { fn area(&self)->u32 { //使用 。运算符通过 self 关键字获取字段的值 self.width * self.height } } fn main() { //实例化结构 let small=Rectangle { width:10, height:20 }; //打印矩形的面积 println!("width is {} height is {} area of Rectangle is {}",small.width,small.height,small.area()); }
width is 10 height is 20 area of Rectangle is 200
静态方法可用作实用程序方法,这些方法甚至在实例化结构之前就已经存在,静态方法是使用结构的名称调用的,无需实例即可访问,与普通方法不同,静态方法不会采用&self参数。
诸如函数之类的静态方法和其他方法可以选择包含参数。
impl Structure_Name { //创建 Point 结构对象的静态方法 fn method_name(param1: datatype, param2: datatype) -> return_type { //逻辑写在这里 } }
structure_name ::语法用于访问静态方法。
structure_name::method_name(v1,v2)
以下示例将 getInstance 方法用作工厂类,该类创建并返回结构 Point 的。
//声明一个结构 struct Point { x: i32, y: i32, } impl Point { //创建 Point 结构对象的静态方法 fn getInstance(x: i32, y: i32) -> Point { Point { x: x, y: y } } //显示结构字段的值 fn display(&self){ println!("x ={} y={}",self.x,self.y ); } } fn main(){ //调用静态方法 let p1=Point::getInstance(10,20); p1.display(); }
x =10 y=20
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)
HarmonyOS快速入门与实战 -〔QCon+案例研习社〕