当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Rust BTreeSet用法及代码示例


本文简要介绍rust语言中 Struct std::collections::btree_set::BTreeSet 的用法。

用法

pub struct BTreeSet<T> { /* fields omitted */ }

基于B-Tree 的集合。

请参阅 BTreeMap 的文档,详细讨论此集合的性能优势和劣势。

以这样一种方式修改项目是一个逻辑错误,即项目相对于任何其他项目的排序(由 Ord 特征确定)在它位于集合中时发生变化。这通常只能通过 Cell RefCell 、全局状态、I/O 或不安全代码实现。未指定由此类逻辑错误导致的行为(它可能包括Panics、不正确的结果、中止、内存泄漏或未终止),但不会是未定义的行为。

例子

use std::collections::BTreeSet;

// Type inference lets us omit an explicit type signature (which
// would be `BTreeSet<&str>` in this example).
let mut books = BTreeSet::new();

// Add some books.
books.insert("A Dance With Dragons");
books.insert("To Kill a Mockingbird");
books.insert("The Odyssey");
books.insert("The Great Gatsby");

// Check for a specific one.
if !books.contains("The Winds of Winter") {
    println!("We have {} books, but The Winds of Winter ain't one.",
             books.len());
}

// Remove a book.
books.remove("The Odyssey");

// Iterate over everything.
for book in &books {
    println!("{}", book);
}

可以从数组初始化具有已知项目列表的BTreeSet

use std::collections::BTreeSet;

let set = BTreeSet::from([1, 2, 3]);

相关用法


注:本文由纯净天空筛选整理自rust-lang.org大神的英文原创作品 Struct std::collections::btree_set::BTreeSet。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。