當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Rust HashSet.replace用法及代碼示例

本文簡要介紹rust語言中 std::collections::hash_set::HashSet.replace 的用法。

用法

pub fn replace(&mut self, value: T) -> Option<T>

向集合中添加一個值,替換等於給定值的現有值(如果有)。返回替換的值。

例子

use std::collections::HashSet;

let mut set = HashSet::new();
set.insert(Vec::<i32>::new());

assert_eq!(set.get(&[][..]).unwrap().capacity(), 0);
set.replace(Vec::with_capacity(10));
assert_eq!(set.get(&[][..]).unwrap().capacity(), 10);

相關用法


注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 std::collections::hash_set::HashSet.replace。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。