📅  最后修改于: 2020-11-02 04:18:20             🧑  作者: Mango
数组用于表示值的同质集合。类似地,结构是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("TutorialsPoint"),
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 TutorialsPoint age is 50
要修改实例,应将实例变量标记为可变的。下面的示例声明并初始化一个名为Employee的结构,然后将age字段的值从50修改为40。
let mut emp1 = Employee {
company:String::from("TutorialsPoint"),
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 TutorialsPoint 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("TutorialsPoint"),
name:String::from("Mohtashim"),
age:50
};
let emp2 = Employee{
company:String::from("TutorialsPoint"),
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 TutorialsPoint age is 50
Name is :Kannan company is TutorialsPoint 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("TutorialsPoint"),
name:String::from("Mohtashim"),
age:50
};
let emp2 = Employee {
company:String::from("TutorialsPoint"),
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 TutorialsPoint age is 50
方法就像函数。它们是编程指令的逻辑组。方法用fn关键字声明。方法的范围在结构块内。
方法在结构块外部声明。 impl关键字用于在结构上下文内定义方法。方法的第一个参数将始终是self ,它表示结构的调用实例。方法对结构的数据成员进行操作。
要调用方法,我们需要首先实例化结构。可以使用结构的实例调用该方法。
struct My_struct {}
impl My_struct {
//set the method's context
fn method_name() {
//define a method
}
}
以下示例定义一个具有字段− width和height的Rectangle结构。方法区域在结构的上下文中定义。 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