Rust development - Struct usage example

structure

1. Definition

pub struct User
{
    
    
    user_id : u32,
    user_name: String,
    is_vip : bool,
}

2. Instantiation
The initialization here must all assign values ​​to all members, unlike C++, which can initialize a value separately

let user : User = User {
    
     user_id: 100, user_name: "matt".to_string(), is_vip: false};

Simplify assignment. When the fields in the structure are the same as the declared assignment fields, the field name can be omitted. The literal value behind is different, so the field name must be specified

	let user_id : u32 = 101;
    let user_name  = "matting".to_string();
    let vip = true;
    let user2:User = User {
    
     user_id, user_name, is_vip:vip};

3. Get the value

let user_name = user.user_name;

4. Variable access must be declared as a variable structure to allow variable access. All elements in the structure are variable.

 let mut user : User = User {
    
     user_id: 100, user_name: "matt".to_string(), is_vip: false};
    
  user.user_name = "matt45m".to_string();

5. Update syntax, when creating a new instance based on an instance, you can use the update syntax. ...user represents the value of the next two fields from the first structure

 	let mut user : User = User {
    
     user_id: 100, user_name: "matt".to_string(), is_vip: false};
    let user_id : u32 = 101;
    
    let user2:User = User {
    
     user_id, ..user};

6. Tuple struct
Tuple struct has a specified name as a whole, but the elements inside have no names.

 struct Color(u8,u8,u8);
 let black = Color(0,0,0);

7. The method of struct (the struct of rust is similar to the class of c++)

  • Methods are defined in the context of struct (enum, trait objects).
  • The first parameter of the method is self, which points to the called struct instance (similar to C++'s this pointer).
  • method defined in the impl (implement) block impl StructName {}
  • The first parameter of a method can be &self, which can also take its ownership or mutable borrow, like any other parameter.
  • The new method can construct a struct, which can be understood as a constructor
pub struct SlotNode
{
    
    
    user_name :String,
    start_node : u32,//开始节点
    end_node : u32,//结束节点
}

impl SlotNode 
{
    
    
    pub fn new() -> Self 
    {
    
    
       SlotNode {
    
     user_name: String::new(), start_node: 0, end_node: 1024}
    }

    pub fn get_end_node(&self) -> u32
    {
    
    
        self.end_node
    }
}

Instantiation and Access

let slot_node = SlotNode::new();
 println!("{}\n", slot_node.get_end_node());

8. Struct inheritance
The inheritance of rust struct is not as powerful as C++, and there are also big differences in concept. In fact, it is not inheritance in the traditional sense.

// 定义一个类似于父类的结构体
#[derive(Debug)]
 struct Animal
{
    
    
    gender: String,
}
impl Animal 
{
    
    
     fn new(gender: String) -> Self 
     {
    
    
         Self {
    
     gender }
     }
 }
 impl Animal 
 {
    
    
     pub fn print_gender(&self) 
     {
    
    
         println!("Animal {}", self.gender);
     }

     fn set_gender(&mut self, gender: String) 
     {
    
    
         self.gender = gender;
     }
 }

// 定义子类
#[derive(Debug)]
struct Cat  
{
    
    
     animal: Animal,
     name: String,
 }
impl Cat  
{
    
    
     fn new(animal: Animal, name: &str) -> Self 
     {
    
    
        Self {
    
     animal , name: name.to_string()}
     }
 }
 impl Cat 
 {
    
    
     fn as_animal(&self) -> &Animal 
     {
    
    
         &self.animal
     }

     fn as_mut_animal(&mut self) -> &mut Animal 
     {
    
    
         &mut self.animal
     }
 }
 
 fn main() {
    
    
    let student = Animal::new("male".to_string());
    let mut tome = Cat ::new(student, "小橘");

    tome.animal.print_gender();
    tome.animal.set_gender("femininity".to_string());
    tome.animal.print_gender();

    println!("{:#?}", tome);

    let a: &Animal = tome.as_animal();
    a.print_gender();
    let a: &mut Animal = tome.as_mut_animal();
    a.set_gender("femininity".to_string());
    a.print_gender();
}

insert image description here

Guess you like

Origin blog.csdn.net/matt45m/article/details/126218188