Rust模塊關(guān)鍵詞和哈希表詳解
模塊
一、pub:公有化
mod front_of_house {
pub mod hosting {
//這里的pub是讓這個模塊公有化,也就是說可以通過 crate::front_of_house::hosting::訪問模塊中的元素
pub fn add_to_waitlist() {}//但是模塊內(nèi)部的元素是否公有化仍然是需要pub關(guān)鍵字指明的,并且我們知道這樣是合理的,因為這樣我們對模塊內(nèi)部的元素哪些需要公開,哪些需要私有有更精準(zhǔn)的把控。
}
}
pub fn eat_at_restaurant() {
// 絕對路徑
crate::front_of_house::hosting::add_to_waitlist();
// 相對路徑
front_of_house::hosting::add_to_waitlist();
}
結(jié)構(gòu)體和枚舉的公有化:
我們也可以用pub來設(shè)計公有的結(jié)構(gòu)體和枚舉,如果我們在結(jié)構(gòu)體的定義前加入了pub關(guān)鍵詞,那么結(jié)構(gòu)體就是公有的,但是內(nèi)部的字段仍然是私有的,也要通過pub來控制哪些要公有哪些要私有。(比如QQ你可以選擇公開自己的性別生日,但是不會公開身份證號((。
super:從父模塊開始的相對路徑
fn deliver_order() {}
mod back_of_house {
fn fix_incorrect_order() {
cook_order();
super::deliver_order();
}
fn cook_order() {}
}依然是作用域的概念,super關(guān)鍵字把路徑的起點返回到了父模塊的作用域,比如上面的代碼本來在模塊back_of_house的作用域內(nèi),super返回到了crate作用域,從而就可以直接訪問deliver_order()了。
二、as提供新名稱:
use std::fmt::Result; use std::io::Result as IoResult
三、使用外部包:
1.在Cargo.toml加入對應(yīng)依賴
rand = "0.8.5"
2.用use引入:
use rand::Rng
fn main() {
let secret_number = rand::thread_rng().gen_range(1..=100);
}嵌套路徑:
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
相當(dāng)于py的字典,不同的是哈希表也是同質(zhì)的,也就是所有的鍵都得是一個類型,所有的值也得是一個類型。
一、新建哈希map:
use std::collections::HashMap;
let mut scores = HashMap::new();
scores.insert(String::from("Blue"), 10);
scores.insert(String::from("Yellow"), 50);
二、訪問哈希map的值:
get
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循環(huán)遍歷:
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}");
}三、所有權(quán):
對于像 i32 這樣的實現(xiàn)了 Copy trait 的類型,其值可以拷貝進(jìn)哈希 map。對于像 String 這樣擁有所有權(quán)的值,其值將被移動而哈希 map 會成為這些值的所有者。
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 不再有效,
// 嘗試使用它們看看會出現(xiàn)什么編譯錯誤!因為涉及所有權(quán)的轉(zhuǎn)移,因此在調(diào)用insert之后,field_name, field_value將不能使用。
如果將值的引用插入哈希map中,這些值本身不會移動到哈希map中,但至少要保證但是這些引用指向的值在哈希 map 有效時也是有效的。
四、更新哈希map:
1.覆蓋:對同一個鍵用兩次insert即可
2.只在鍵尚未存在時插入鍵值對:(如果鍵存在不進(jìn)行任何操作,如果不存在則連同值一起插入)
方法是調(diào)用entryAPI,返回值是一個枚舉Entry,代表了可能存在也可能不存在的值。
Entry的or_insert方法在鍵對應(yīng)的值存在的時候會返回鍵的可變引用,不存在則把參數(shù)插入。
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返回的可變引用可以用于更新舊值,具體如下:
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:?}");到此這篇關(guān)于Rust模塊關(guān)鍵詞和哈希表詳解的文章就介紹到這了,更多相關(guān)Rust模塊關(guān)鍵詞和哈希表內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解讀Rust的Rc<T>:實現(xiàn)多所有權(quán)的智能指針方式
Rc<T> 是 Rust 中用于多所有權(quán)的引用計數(shù)類型,通過增加引用計數(shù)來管理共享數(shù)據(jù),只有當(dāng)最后一個引用離開作用域時,數(shù)據(jù)才會被釋放,Rc<T> 適用于單線程環(huán)境,并且只允許不可變共享數(shù)據(jù);需要可變共享時應(yīng)考慮使用 RefCell<T> 或其他解決方案2025-02-02

