[rust-dev] simple hash map for storing structs that contain ~str and/or ~[]

Tim Taubert ttaubert at mozilla.com
Mon Dec 17 06:42:27 PST 2012


I'm trying to create simple hash map with an interface like this:

trait Map<K:Eq Hash, V> {
  fn find(&const self, k: &K) -> Option<V>;
  fn insert(&mut self, k: K, +v: V);
  fn remove(&mut self, k: &K);
}

The implementation itself is subject to change and I'll experiment with
that a bit. I copied some of the parts from send_map and tried the
following:

struct Item {
  v: uint
}

fn test() {
  let item = Item {v: 0};
  let mut map = LinearMap::new();
  map.insert(~"foo", item);
  assert map.find(&~"foo") == Some(item);
}

This works fine until I try to store structs that contain a ~str or ~[].
I'm getting this error:

warning: instantiating copy type parameter with a not implicitly
copyable type
assert map.find(&~"foo") == None;

Is there anything I can do about this other than using managed boxes? I
really don't want to force people to use GC.


Thanks,

- Tim


More information about the Rust-dev mailing list