[Rust]模块关键词和哈希表

画师:竹取工坊

大佬们好!我是Mem0rin!现在正在准备自学转码。

如果我的文章对你有帮助的话,欢迎关注我的主页Mem0rin,欢迎互三,一起进步!

文章目录

模块

一、pub:公有化

rust 复制代码
mod front_of_house {
    pub mod hosting {
        //这里的pub是让这个模块公有化,也就是说可以通过 crate::front_of_house::hosting::访问模块中的元素
        pub fn add_to_waitlist() {}//但是模块内部的元素是否公有化仍然是需要pub关键字指明的,并且我们知道这样是合理的,因为这样我们对模块内部的元素哪些需要公开,哪些需要私有有更精准的把控。
    }
}
pub fn eat_at_restaurant() {
    // 绝对路径
    crate::front_of_house::hosting::add_to_waitlist();
    // 相对路径
    front_of_house::hosting::add_to_waitlist();
}

结构体和枚举的公有化:

我们也可以用pub来设计公有的结构体和枚举,如果我们在结构体的定义前加入了pub关键词,那么结构体就是公有的,但是内部的字段仍然是私有的,也要通过pub来控制哪些要公有哪些要私有。(比如QQ你可以选择公开自己的性别生日,但是不会公开身份证号((。

super:从父模块开始的相对路径

rust 复制代码
fn deliver_order() {}

mod back_of_house {
    fn fix_incorrect_order() {
        cook_order();
        super::deliver_order();
    }
    
    fn cook_order() {}
}

依然是作用域的概念,super关键字把路径的起点返回到了父模块的作用域,比如上面的代码本来在模块back_of_house的作用域内,super返回到了crate作用域,从而就可以直接访问deliver_order()了。

二、 as提供新名称:

rust 复制代码
use std::fmt::Result;
use std::io::Result as IoResult

三、使用外部包:

1.在Cargo.toml加入对应依赖

复制代码
rand = "0.8.5"

2.用use引入:

rust 复制代码
use rand::Rng

fn main() {
    let secret_number = rand::thread_rng().gen_range(1..=100);
}

嵌套路径:

rust 复制代码
use std::cmp::Ordering;
use std::io;
//等价于
use std::{cmp::Ordering, io};

use std::io;
use std::io::Write;
//等价于
use std::io::{self, Write};

HashMap

相当于py的字典,不同的是哈希表也是同质的,也就是所有的键都得是一个类型,所有的值也得是一个类型。

一、新建哈希map:

rust 复制代码
use std::collections::HashMap;
let mut scores = HashMap::new();
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);

二、访问哈希map的值:

get

rust 复制代码
use std::collections::HashMap;
let mut scores = HashMap::new();
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);
let team_name = String::from("Blue");
let score = scores.get(&team_name).copied().unwrap_or(0);

for循环遍历:

rust 复制代码
use std::collections::HashMap;

let mut scores = HashMap::new();

scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);

for (key, value) in &scores {
    println!("{key}: {value}");
}

三、所有权:

对于像 i32 这样的实现了 Copy trait 的类型,其值可以拷贝进哈希 map。对于像 String 这样拥有所有权的值,其值将被移动而哈希 map 会成为这些值的所有者。

rust 复制代码
use std::collections::HashMap;

let field_name = String::from("Favorite color");
let field_value = String::from("Blue");

let mut map = HashMap::new();
map.insert(field_name, field_value);
// 这里 field_name 和 field_value 不再有效,
// 尝试使用它们看看会出现什么编译错误!

因为涉及所有权的转移,因此在调用insert之后,field_name, field_value将不能使用。

如果将值的引用插入哈希map中,这些值本身不会移动到哈希map中,但至少要保证但是这些引用指向的值在哈希 map 有效时也是有效的。

四、更新哈希map:

1.覆盖:对同一个键用两次insert即可

2.只在键尚未存在时插入键值对:(如果键存在不进行任何操作,如果不存在则连同值一起插入)

方法是调用entryAPI,返回值是一个枚举Entry,代表了可能存在也可能不存在的值。

Entry的or_insert方法在键对应的值存在的时候会返回键的可变引用,不存在则把参数插入。

rust 复制代码
use std::collections::HashMap;
let mut scores = HashMap::new();
scores.insert(String::from("Blue"), 10);
scores.entry(String::from("Yellow")).or_insert(50);
scores.entry(String::from("Blue")).or_insert(50);
println!("{scores:?}");

or_insert返回的可变引用可以用于更新旧值,具体如下:

rust 复制代码
use std::collections::HashMap;

let text = "hello world wonderful world";

let mut map = HashMap::new();
for word in text.split_whitespace() {
    let count = map.entry(word).or_insert(0);
    *count += 1;
}

println!("{map:?}");
相关推荐
l1t37 分钟前
pgrust v0.4-beta 安装和查询测试
数据库·postgresql·rust·duckdb·pgrust
Lhan.zzZ42 分钟前
Qt:动态库模块框架设计
开发语言·c++·qt
quantdash_cc1 小时前
如何从多只股票的历史行情构建收益率矩阵?Python 与 QuantDash 实战教程
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
不是万能的豆学长5 小时前
filetidy 文件整理助手:一键归类 + 规范重命名(Python 零依赖单文件工具)
大数据·开发语言·python·技术美术
懂点Tech9 小时前
ctfshow MISC入门图片篇(信息附加):misc41解题思路
开发语言·python
运维开发笔记9 小时前
5.Rust 向量(Vec)入门指南
rust
catchadmin10 小时前
PHP 8.7 协程新提案 让跨协程代码共享请求身份
开发语言·php
念何架构之路10 小时前
go-grpc设计思想
开发语言·后端·golang
酷炫码神11 小时前
Day 10|Pinia 状态管理:把散落在各组件的数据,收进一个全局仓库
android·开发语言·javascript
Elastic 中国社区官方博客12 小时前
我们如何通过 70 多个拉取请求,将 OpenTelemetry Java agent 迁移到 invokedynamic
java·大数据·开发语言·elasticsearch·搜索引擎·全文检索·es