[][src]Function tao_of_rust::ch02::collections::hashset_btreeset

pub fn hashset_btreeset()

集合: HashSet和BTreeSet

Basic usage:

use std::collections::HashSet;
use std::collections::BTreeSet;
fn hashset_btreeset() {
    let mut hbooks = HashSet::new();
    let mut bbooks = BTreeSet::new();

    // 插入数据
    hbooks.insert("A Song of Ice and Fire");
    hbooks.insert("The Emerald City");
    hbooks.insert("The Odyssey");

    // 判断元素是否存在,contains方法和HashMap中的一样
    if !hbooks.contains("The Emerald City") {
        println!("We have {} books, but The Emerald City ain't one.",
                 hbooks.len());
    }
    // 顺序是随机 {"The Emerald City", "The Odyssey", "A Song of Ice and Fire"}
    println!("{:?}", hbooks);

    bbooks.insert("A Song of Ice and Fire");
    bbooks.insert("The Emerald City");
    bbooks.insert("The Odyssey");
    // 顺序永远是  {"A Song of Ice and Fire", "The Emerald City", "The Odyssey"}
    println!("{:?}", bbooks);
}
hashset_btreeset();Run