美文网首页
rust数据类型

rust数据类型

作者: Wu杰语 | 来源:发表于2022-01-13 21:15 被阅读0次

rust数据类型支持primitive和compound类型,见下图


数据类型

primitive类型

#![feature(core_intrinsics)]
fn print_type_of<T>(_: T) {
    println!("{}", unsafe { std::intrinsics::type_name::<T>() });
}

fn main() {
    println!("Hello, world!");
    let logical: bool = true;
    print_type_of(logical);
    
    let mut inferred_type = 12;
    print_type_of(inferred_type);
    
    inferred_type = 4294967296i64;
    print_type_of(inferred_type);
    
    let t = "a";
    print_type_of(t);
    
    let c = 'a';
    print_type_of(c);
   
}

compound数据类型

#![feature(core_intrinsics)]
fn print_type_of<T>(_: T) {
    println!("{}", unsafe { std::intrinsics::type_name::<T>() });
}

fn main() {
   
    let v = [1,2,3];
    print_type_of(v);
    
    let q:[i64; 3] = [1, 2, 3];
    print_type_of(q);
    
    let x: (i32, f64, u8) = (500, 6.4, 1);
    print_type_of(x);
    
    println!("{}", x.0);
    println!("{}", x.2);
    
    let a = [3; 5];
    print_type_of(a);
    
  //单元结构体
    struct Empty;
    let e = Empty;
    print_type_of(e);
    
    // 元组结构体
    struct TT(i32, i32, i32);
    let col = TT(0, 2, 4);
    print_type_of(col);
    
    // 具名结构体
    struct People {
        name: &'static str,
        qender: i32,
    }
    let pe = People{name:"tt", qender:3};
    print_type_of(pe);
    
    enum Number {
        Zero,
        One,
        Two,
    }
    let n = Number::One;
    print_type_of(n)
}

collections

参见https://doc.rust-lang.org/std/index.html#containers-and-collections,这里定义了collections的几个关键元素
The option and result modules define optional and error-handling types, Option<T> and Result<T, E>. The iter module defines Rust’s iterator trait, Iterator, which works with the for loop to access collections.

其中有几个基本元素需要注意定义

pub enum Option<T> {
    None,
    Some(T),
}

enum Result<T, E> {
   Ok(T),
   Err(E),
}

// iterator有70个左右的定义
pub trait Iterator {
    type Item;
Show 70 methods}

collections包括:

  • Sequences: Vec, VecDeque, LinkedList
  • Maps: HashMap, BTreeMap
  • Sets: HashSet, BTreeSet
  • Misc: BinaryHeap

列举几个例子

vec

参考 https://doc.rust-lang.org/std/vec/struct.Vec.html#

fn main() {
    println!("Hello, world!");
    
    let mut vec = Vec::new();
    vec.push(1);
    vec.push(2);
    
    println!("{}", vec.len());
    let p = vec.pop();
    if (p.is_some()) {
        println!("{}", p.unwrap());
    }
}
HashMap
use std::collections::HashMap;

fn main() {
    let mut map = HashMap::new();
    map.insert("a", 1);
    map.insert("b", 2);
    map.insert("c", 3);
    
    for (key, val) in map.iter() {
        println!("key: {} val: {}", key, val);
    }
}

小结

rust的数据类型还是比较丰富,融合了面向对象、函数式各种语言范式的长处。

相关文章

  • 读Rust程序设计语言 - 04

    语言/Rust 数据类型 - Rust 程序设计语言 简体中文版 数据类型 rust 声明的变量一定属于某一数据类...

  • rust数据类型

    rust数据类型支持primitive和compound类型,见下图 primitive类型 compound数据...

  • Rust 数据类型

    在 Rust 中,每一个值都属于某一个 数据类型(data type),这告诉 Rust 它被指定为何种数据,以便...

  • RUST 学习日记 第11课 ——向量

    RUST 学习日记 第11课 ——向量(动态数组) 0x00 回顾与开篇 上一节主要讲解了Rust的复合数据类型...

  • Rust—数据类型

    Rust是静态类型语言,在编译时必须知道所有变量的类型,根据值及其使用方式,编译器通常可以推断出我们想要的类型。当...

  • Rust - 数据类型

    基础类型有integerfloatbooleanchar 0x01 integer 类型定义 值定义 包含了八进制...

  • Rust 数据类型

    数据类型 rust 是静态类型语言,所有变量在编译时都需要明确类型。 整型 长度无符号有符号8-bitu8i816...

  • RUST 学习日记 第17课 ——流程控制

    RUST 学习日记 第17课 ——流程控制 0x00 回顾与开篇 有关Rust数据类型的知识暂时告一段落了,从这...

  • Rust基础学习-03-变量常量和基本数据类型

    这里来聊一下Rust的变量声明,以及基本的数据类型。 可变性 与其他编程语言很不同的一点是,Rust 的变量默认是...

  • 【Rust】Rust和Java的对比--数据类型

    Rust和Java都是一种静态类型的语言。这意味着它必须在编译期知道所有变量的类型。 整形 Rust 长度有符号类...

网友评论

      本文标题:rust数据类型

      本文链接:https://www.haomeiwen.com/subject/lctscrtx.html