wxvirus wxvirus
首页
  • Go文章

    • Go语言学习
  • Rust

    • Rust学习
  • Java

    • 《Java》
  • Python文章

    • Python
  • PHP文章

    • PHP设计模式
  • 学习笔记

    • 《Git》
  • HTML
  • CSS
  • JS
  • 技术文档
  • GitHub技巧
  • 刷题
  • 博客搭建
  • 算法学习
  • 架构设计
  • 设计模式
  • 学习
  • 面试
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

无解的lifecycle

let today = new Beginning()
首页
  • Go文章

    • Go语言学习
  • Rust

    • Rust学习
  • Java

    • 《Java》
  • Python文章

    • Python
  • PHP文章

    • PHP设计模式
  • 学习笔记

    • 《Git》
  • HTML
  • CSS
  • JS
  • 技术文档
  • GitHub技巧
  • 刷题
  • 博客搭建
  • 算法学习
  • 架构设计
  • 设计模式
  • 学习
  • 面试
  • 实用技巧
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • C&C++

  • PHP

  • Python

  • Go

  • microservice

  • rust

    • windows安装和配置
    • rust的第一个程序
    • cargo
    • 变量和基本类型
    • 函数和模块
    • 函数参数传递
    • 表达式和语句以及判断
    • 字符串练习
    • 自定义函数和模块的使用
    • 结构体入门和基本写法
    • trait入门
    • 生命周期
    • Vector快速入门
    • 所有权以及所有权转移
    • 宏入门
    • 在struct中使用泛型
      • 泛型
      • "struct 类"方法中使用泛型
  • Java

  • 学习笔记

  • 后端
  • rust
wxvirus
2023-01-04

在struct中使用泛型

# 在 struct 中使用泛型

# 泛型

定义不局限名称,可以写任意的一个字母,最好是大写,因为从编程以来,认识的使用泛型的基本都是使用的大写字母,但是肯定会有一对<>进行包裹。

背景:

现在有一个积分平台,老同学使用的是i32类型作为积分的类型;现在有一个需求要支持f32类型的,这就需要泛型来支持。

user_model.rs

pub struct UserScore<A, B> {
    // 泛型
    pub user_id: A,
    pub score: B,
    pub comment: &'static str,
}

pub fn new_user_score_a() -> UserScore<i32, i32> {
    UserScore {
        user_id: 0,
        score: 0,
        comment: "A类用户",
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14

这里的A和B可以自己定义名称,但是如果使用了,就得保持一致

使用

main.rs

use models::user_model::{new_user_score_a, new_user_score_b};

// mod lib; // lib.rs 或者 lib/mod.rs
mod models;

fn main() {

    let mut user_score_a = new_user_score_a();
    user_score_a.user_id = 101;
    user_score_a.score = 10;
    println!("{:?}", user_score_a);

    let mut user_score_b = new_user_score_b();
    user_score_b.user_id = "#EFff";
    user_score_b.score = 10.0;
    println!("{:?}", user_score_b);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

打印结果:

cargo build && cargo run

UserScore { user_id: 101, score: 10, comment: "A类用户" }
UserScore { user_id: "#EFff", score: 10.0, comment: "B类用户" }
1
2
3
4

# "struct 类"方法中使用泛型

基于上面的代码,我们新建类方法

impl<A, B> UserScore<A, B> {
    pub fn get_user_type(&self) -> &'static str {
        // 表达式
        &self.comment
    }

    pub fn get_user_id(&self) -> &A {
        &&self.user_id
    }
}
1
2
3
4
5
6
7
8
9
10

这样无论你调用的时候传入的是什么类型,这里都有与之对应的类型结果

use models::user_model::{new_user_info, new_user_score_a, new_user_score_b, UserInfo};

// mod lib; // lib.rs 或者 lib/mod.rs
mod models;

fn main() {

    let mut user_score = new_user_score_a();
    user_score.user_id = 101;
    user_score.score = 10;
    println!("{:?}", user_score);
    println!("{:?}", user_score.get_user_type());
    println!("{:?}", user_score.get_user_id());

    let mut user_score_b = new_user_score_b();
    user_score_b.user_id = "#EFff";
    user_score_b.score = 10.0;
    println!("{:?}", user_score_b);
    println!("{:?}", user_score_b.get_user_type());
    println!("{:?}", user_score_b.get_user_id());
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
cargo build && cargo run

UserScore { user_id: 101, score: 10, comment: "A类用户" }
"A类用户"
101
UserScore { user_id: "#EFff", score: 10.0, comment: "B类用户" }
"B类用户"
"#EFff"
1
2
3
4
5
6
7
8
编辑 (opens new window)
#generic
上次更新: 2023/01/04, 22:49:36
宏入门
《Java》学习

← 宏入门 《Java》学习→

最近更新
01
centos7安装redis6文档记录
02-14
02
portainer的安装
02-11
03
gin自定义验证器和翻译器
02-11
更多文章>
Theme by Vdoing | Copyright © 2021-2023 wxvirus
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式