Rust-structure
錆-構造
配列は、値の同種のコレクションを表すために使用されます。 同様に、構造体は、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("finddevguides"),
name:String::from("Mohtashim"),
age:50
};
println!("Name is :{} company is {} age is {}",emp1.name,emp1.company,emp1.age);
}
上記の例では、名前、会社、タイプの年齢の3つのフィールドを持つEmployee構造体を宣言しています。 main()は構造を初期化します。 printlnを使用します! 構造で定義されたフィールドの値を印刷するマクロ。
出力
Name is :Mohtashim company is finddevguides age is 50
構造体インスタンスの変更
インスタンスを変更するには、インスタンス変数を可変とマークする必要があります。 次の例では、_Employee_という名前の構造体を宣言して初期化し、後で_age_フィールドの値を50から40に変更します。
let mut emp1 = Employee {
company:String::from("finddevguides"),
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 finddevguides age is 40
構造体を関数に渡す
次の例は、構造体のインスタンスをパラメーターとして渡す方法を示しています。 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("finddevguides"),
name:String::from("Mohtashim"),
age:50
};
let emp2 = Employee{
company:String::from("finddevguides"),
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 finddevguides age is 50
Name is :Kannan company is finddevguides age is 32
関数から構造体を返す
関数_who_is_elder()_を考えてみましょう。この関数は、2人の従業員の年齢を比較し、年上の従業員を返します。
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("finddevguides"),
name:String::from("Mohtashim"),
age:50
};
let emp2 = Employee {
company:String::from("finddevguides"),
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 finddevguides 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_は、構造のコンテキスト内で定義されます。 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)
図
次の例は、_Point_構造のインスタンスを作成して返すファクトリクラスとして_getInstance_メソッドを使用します。
//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