zoukankan      html  css  js  c++  java
  • [易学易懂系列|rustlang语言|零基础|快速入门|(11)|Structs结构体]

    [易学易懂系列|rustlang语言|零基础|快速入门|(11)]

    有意思的基础知识

    Structs

    我们今天来看看数据结构:structs.

    简单来说,structs,就是用来封装相关数据的一种数据类型。

    一般来说, struct 的命名方式为驼峰方式如: CamelCase

    在Rust有以下几种定义struct的方式:

    1. C-like structs (C语言形式的struct)
    2. Tuple structs (元组形式的struct)
    3. Unit structs (单元struct)

    在Rust,面向对象抽象,一般由struct和trait(特征)来实现,即structs来

    封装属性数据,而trait用来封装方式,再由impl来关联。

    我们来看看代码:

    C语言形式的struct

    // Struct Declaration
    struct Color {
        red: u8,
        green: u8,
        blue: u8
    }
    
    fn main() {
      // Creating an instance
      let black = Color {red: 0, green: 0, blue: 0};
    
      // Accessing its fields using dot notation
      println!("Black = rgb({}, {}, {})", black.red, black.green, black.blue); //Black = rgb(0, 0, 0)
    
      // Structs are immutable by default, use `mut` to make it mutable but doesn't support field level mutability
      let mut link_color = Color {red: 0,green: 0,blue: 255};
      link_color.blue = 238;
      println!("Link Color = rgb({}, {}, {})", link_color.red, link_color.green, link_color.blue); //Link Color = rgb(0, 0, 238)
    
      // Copy elements from another instance
      let blue = Color {blue: 255, .. link_color};
      println!("Blue = rgb({}, {}, {})", blue.red, blue.green, blue.blue); //Blue = rgb(0, 0, 255)
    
      // Destructure the instance using a `let` binding, this will not destruct blue instance
      let Color {red: r, green: g, blue: b} = blue;
      println!("Blue = rgb({}, {}, {})", r, g, b); //Blue = rgb(0, 0, 255)
    
      // Creating an instance via functions & accessing its fields
      let midnightblue = get_midnightblue_color();
      println!("Midnight Blue = rgb({}, {}, {})", midnightblue.red, midnightblue.green, midnightblue.blue); //Midnight Blue = rgb(25, 25, 112)
    
      // Destructure the instance using a `let` binding
      let Color {red: r, green: g, blue: b} = get_midnightblue_color();
      println!("Midnight Blue = rgb({}, {}, {})", r, g, b); //Midnight Blue = rgb(25, 25, 112)
    }
    
    fn get_midnightblue_color() -> Color {
        Color {red: 25, green: 25, blue: 112}
    }
    

    元组形式的struct

    struct Color(u8, u8, u8);
    struct Kilometers(i32);
    
    fn main() {
      // Creating an instance
      let black = Color(0, 0, 0);
    
      // Destructure the instance using a `let` binding, this will not destruct black instance
      let Color(r, g, b) = black;
      println!("Black = rgb({}, {}, {})", r, g, b); //black = rgb(0, 0, 0);
    
      // Newtype pattern
      let distance = Kilometers(20);
      // Destructure the instance using a `let` binding
      let Kilometers(distance_in_km) = distance;
      println!("The distance: {} km", distance_in_km); //The distance: 20 km
    }
    

    单元struct

    struct Electron;
    
    fn main() {
      let x = Electron;
    }
    

    以上,希望对你有用。

    如果遇到什么问题,欢迎加入:rust新手群,在这里我可以提供一些简单的帮助,加微信:360369487,注明:博客园+rust
    

    https://learning-rust.github.io/docs/b2.structs.html

  • 相关阅读:
    [置顶] 文件批量重命名(工具)
    zookeeper源码分析之三客户端发送请求流程
    java set转list,数组与list的转换
    分布式电子邮件系统设计--转载
    redis 模糊删除实现
    eclipse 使用jetty调试时,加依赖工程的源码调试方法
    社交产品后端架构设计--转载
    solr服务器的查询过程
    What is corresponding Cron expression to fire in every X seconds, where X > 60? --转载
    zookeeper源码分析之二客户端启动
  • 原文地址:https://www.cnblogs.com/gyc567/p/11961235.html
Copyright © 2011-2022 走看看