[rust-dev] strings in sets/maps

Clark Gaebel cg.wowus.cg at gmail.com
Fri Jun 6 06:42:32 PDT 2014


&str and string are "equivalent", so use the _equiv version of functions
you need. I'll send a patch to better-document this common use case later
today.
On 2014-06-06 9:40 AM, "Diggory Hardy" <lists at dhardy.name> wrote:

>  Dear List,
>
>
>
> I want to use strings as map keys, but couldn't find any mention of this
> in my understanding common use-case. The following works but as far as I
> understand requires a copy of the potential key to be made to call
> `contains()`, is this correct?
>
>
>
> let mut set: HashSet<String> = HashSet::new();
>
> set.insert( "x".into_string() );
>
> println!( "set contains x: {}", set.contains( &"x".into_string() ) );
>
>
>
> Note: I would normally be storing/testing &str types with non-static
> lifetime, but I don't think this makes a difference.
>
>
>
> I notice that HashSet<&str> also works (if lifetimes of the inserted
> strings are sufficient).
>
>
>
> Regards,
>
> Diggory Hardy
>
> _______________________________________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140606/a9b4dae1/attachment.html>


More information about the Rust-dev mailing list