Rust - 结构
-
简述
数组用于表示同构值的集合。类似地,结构是 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 }; //NOTE the semicolon Syntax: Accessing values in a structure Use the dot notation to access value of a specific field. instance_name.field1 Illustration struct Employee { name:String, company:String, age:u32 } fn main() { let emp1 = Employee { company:String::from("Jc2182"), name:String::from("Mohtashim"), age:50 }; println!("Name is :{} company is {} age is {}",emp1.name,emp1.company,emp1.age); }
上面的示例声明了一个结构体 Employee,其中包含三个字段——名称、公司和类型的年龄。main() 初始化结构。它使用 println! 宏来打印结构中定义的字段的值。输出
Name is :Mohtashim company is Jc2182 age is 50
-
修改结构体实例
要修改实例,实例变量应标记为可变的。下面的示例声明并初始化了一个名为Employee的结构,然后将age字段的值从 50修改为 40。let mut emp1 = Employee { company:String::from("Jc2182"), name:String::from("Mohtashim"), age:50 }; emp1.age = 40; println!("Name is :{} company is {} age is {}",emp1.name,emp1.company,emp1.age);
输出
Name is :Mohtashim company is Jc2182 age is 40
-
将结构体传递给函数
以下示例显示如何将 struct 的实例作为参数传递。display 方法将 Employee 实例作为参数并打印详细信息。fn display( emp:Employee) { println!("Name is :{} company is {} age is {}",emp.name,emp.company,emp.age); }
这是完整的程序 -//declare a structure struct Employee { name:String, company:String, age:u32 } fn main() { //initialize a structure let emp1 = Employee { company:String::from("Jc2182"), name:String::from("Mohtashim"), age:50 }; let emp2 = Employee{ company:String::from("Jc2182"), name:String::from("Kannan"), age:32 }; //pass emp1 and emp2 to display() display(emp1); display(emp2); } // fetch values of specific structure fields using the // operator and print it to the console fn display( emp:Employee){ println!("Name is :{} company is {} age is {}",emp.name,emp.company,emp.age); }
输出
Name is :Mohtashim company is Jc2182 age is 50 Name is :Kannan company is Jc2182 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() { //initialize structure let emp1 = Employee{ company:String::from("Jc2182"), name:String::from("Mohtashim"), age:50 }; let emp2 = Employee { company:String::from("Jc2182"), name:String::from("Kannan"), age:32 }; let elder = who_is_elder(emp1,emp2); println!("elder is:"); //prints details of the elder employee display(elder); } //accepts instances of employee structure and compares their age fn who_is_elder (emp1:Employee,emp2:Employee)->Employee { if emp1.age>emp2.age { return emp1; } else { return emp2; } } //display name, comapny and age of the employee fn display( emp:Employee) { println!("Name is :{} company is {} age is {}",emp.name,emp.company,emp.age); } //declare a structure struct Employee { name:String, company:String, age:u32 }
输出
elder is: Name is :Mohtashim company is Jc2182 age is 50
-
结构方法
方法就像函数。它们是一组逻辑编程指令。方法是用fn关键词。方法的范围在结构块内。方法在结构块之外声明。这impl关键字用于在结构上下文中定义方法。方法的第一个参数将始终是self,表示结构的调用实例。方法对结构的数据成员进行操作。要调用一个方法,我们需要首先实例化结构。可以使用结构的实例调用该方法。句法
struct My_struct {} impl My_struct { //set the method's context fn method_name() { //define a method } }
说明
下面的例子定义了一个结构Rectangle与字段 - width和height。方法区是在结构的上下文中定义的。area 方法通过self关键字访问结构的字段并计算矩形的面积。//define dimensions of a rectangle struct Rectangle { width:u32, height:u32 } //logic to calculate area of a rectangle impl Rectangle { fn area(&self)->u32 { //use the . operator to fetch the value of a field via the self keyword self.width * self.height } } fn main() { // instanatiate the structure let small = Rectangle { width:10, height:20 }; //print the rectangle's area 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 { //static method that creates objects of the Point structure fn method_name(param1: datatype, param2: datatype) -> return_type { // logic goes here } }
语法 - 调用静态方法
structure_name ::语法用于访问静态方法。structure_name::method_name(v1,v2)
说明
下面的示例使用getInstance方法作为创建并返回结构Point实例的工厂类。//declare a structure struct Point { x: i32, y: i32, } impl Point { //static method that creates objects of the Point structure fn getInstance(x: i32, y: i32) -> Point { Point { x: x, y: y } } //display values of the structure's field fn display(&self){ println!("x ={} y={}",self.x,self.y ); } } fn main(){ // Invoke the static method let p1 = Point::getInstance(10,20); p1.display(); }
输出
x =10 y=20