我有一个BTreeMap
使用二元组键 存储项目的位置,(String, Vec<i32>)
因此存储整数的映射类型是BTreeMap<(String, Vec<i32>), i32>
。
如何通过使用二元组中的第一个元素来获取范围,同时忽略 Vector 的内容(或者 slice &[i32]
)?
一个简短的例子:
//use core::slice::SlicePattern;
use std::collections::BTreeMap;
fn main() {
let mut m : BTreeMap<(String, Vec<i32>), i32> = BTreeMap::new();
m.insert(("hello".to_string(), vec![12]), 34);
m.insert(("hello".to_string(), vec![4]), 56);
m.insert(("other".to_string(), vec![4]), 44);
let res_first = m.get(&("hello".to_string(), vec![12]));
println!("{:?}", res_first);
// not working, and not intuitive range
for (k, v) in m.range(("hello".to_string(), [])..=("g".to_string(), [])) {
println!("key {:?}", k);
}
}
有没有办法使用切片模式或其他东西来使范围查询与向量或切片一起使用?
或类似的东西:
m.range(("hello".to_string(), [])..=("hello".to_string(), [RangeFull]))
我之前的问题得到了很好的答案,但它仅适用于标量,不适用于向量或切片。
您可以以相同的方式执行此操作,只是您需要一个结尾是排他的范围,因为您无法(轻松)表达最大可能的
Vec<i32>
。您可以Vec
为范围的两端指定一个空,并在范围的末尾使用下一个“最大”字符串,该字符串与添加了 nul 字符的同一字符串相同。 (感谢菲利普提出这个想法。)(游乐场)