将结构插入映射而不复制它时,如何使用结构的成员作为其自己的键? [英] How to use a struct's member as its own key when inserting the struct into a map without duplicating it?

查看:28
本文介绍了将结构插入映射而不复制它时,如何使用结构的成员作为其自己的键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将结构插入映射中,其中键归被插入的值所有?

Is it possible to insert a struct into a map where the key is owned by the value being inserted?

在 C 中使用哈希映射时,这是我习惯做的事情.

When using hash-maps in C, this is something which I'm used to doing.

伪代码示例:

struct MyStruct {
    pub map: BTreeMap<&String, StructThatContainsString>,
    // XXX            ^ Rust wants lifetime specified here!
}

struct StructThatContainsString {
    id: String,
    other_data: u32,
}

fn my_fn() {
    let ms = MyStruct { map: BTreeMap::new() };

    let item = StructThatContainsString {
        id: "Some Key".to_string(),
        other_data: 0,
    }

    ms.insert(&item.id, item);
}

如何正确处理这种情况?

How can this situation be correctly handled?

  • 如果这是不可能的,是否可以反过来做,其中值保存对键的引用,该键将是 String ?

另一种可能是使用 set 而不是 map,然后将整个 struct 存储为键,但是在比较 时只使用它的值之一(看起来它会起作用,但如果你想在其他上下文中比较 struct 可能会适得其反).

An alternative could be to use a set instead of a map, then store the entire struct as the key, but only use one of its values when comparing (seems like it would work, but could backfire if you wanted to compare the struct in other contexts).

推荐答案

它不适用于普通引用:

let item = StructThatContainsString {
    id: "Some Key".to_string(),
    other_data: 0,
}

ms.insert(&item.id, item);

item移动到地图中,所以不能有任何待处理的借阅/引用.

item is moved into the map, so there can't be any pending borrows/references.

此外,像get_mut() 会变得危险或不可能,因为它会让您修改具有未完成引用的项目.

Also, methods like get_mut() would become dangerous or impossible, as it would let you modify the item that has an outstanding reference.

假设这样做的原因是为了节省空间,显而易见的选择是:

Assuming the reason for wanting to do this is to save space, the obvious options are:

  • 从值结构中取出键.如果你同时需要它,你要么在地图中查找键时得到它,要么迭代器同时包含键和值:

  • Take the key out of the value struct. If you need it at the same time, you've either got it when looking up a key in the map, or the iterators include both key and value:

struct OnlyKey {
    id: String,
}

struct OnlyValue {
    other_data: u32,
}

这可以通过适当的方法进行清理,以拆分/重新组合各个部分.

This can be cleaned up with appropriate methods to split apart / recombine the various pieces.

使用类似 Rc 的东西作为值的关键部分.Rc 实现 Ord(BTreeMap 需要),如果 T 实现.

Use something like Rc for the key part of the value. Rc<T> implements Ord (required for BTreeMap) if T does.

struct StructThatContainsString {
    id: Rc<String>,
    other_data: u32,
}

这篇关于将结构插入映射而不复制它时,如何使用结构的成员作为其自己的键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆