4 ms·
Yea. I'm using Rust at the moment for fun and the amount of things not in the standard library is crazy to me. What? There is no built-in dictionary? What do I
by new4thaccount 7y ago
Yea. I'm using Rust at the moment for fun and the amount of things not in the standard library is crazy to me. What? There is no built-in dictionary? What do I use instead and where is it?
Edit: based off of all the replies below, everyone understands the validity of what I'm trying to say, but also have fortunately pointed out my admittedly grevious error of not knowing you can just import hashmap from stdlib. The extra step is pretty minimal and not a problem. I'm hoping this is covered in the Rust book.
- resoluteteeth 7y agoYeah Rust is pretty extreme. I was just trying to generate a random number in it and was pretty surprised to find out that at some point it did have this functionality in the standard library but they actually removed it and now it's a separate crate.
- nicoburns 7y agoThat is actually being considered for inclusion at some point. It was mainly excluded due it not being ready and them not wanting to commit to the existing API. That said, I like the Rust way. Including a library is pretty easy, and it mains you get the best API as the main one (rather than a stdlib function that is "good enough", and another library to use if you really care about that functionality).
- jzoch 7y agoYou mean a map? It's in the standard library.
- new4thaccount 7y agoYep. Map/Hash/Dictionary. I didn't know it was included in stdlib (I really thought it wasn't) and feel idiotic now. It is still odd (having a primarily scripting background and not coming from the systems side) that I have to include what seems to be essentially an import statement at the top. I guess it is a lot more efficient that way though. Thanks for pointing out my error!
- jgibson 7y agoEr, doesn't hashmap do pretty much the same thing? https://doc.rust-lang.org/std/collections/struct.HashMap.html https://doc.rust-lang.org/std/collections/struct.HashMap.htm...
- nicoburns 7y agoDictionary in the python sense? There are two! https://doc.rust-lang.org/std/collections/struct.HashMap.html https://doc.rust-lang.org/std/collections/struct.HashMap.htm... https://doc.rust-lang.org/std/collections/struct.BTreeMap.html https://doc.rust-lang.org/std/collections/struct.BTreeMap.ht...
- new4thaccount 7y agoAt least the first one seems pretty straightforward and thanks for replying. I only just started and have a lot to learn.
- StavrosK 7y agoOof, I wasted a good hour+ trying to convert from one to the other. Maybe there's an easy way to do this, but not many people on IRC knew (or were available at the time).
- edflsafoiewq 7y agouse std::iter::FromIterator; BTreeMap::from_iter(hash_map.into_iter())
- steveklabnik 7y agoOr .into_iter().collect(), I believe. No import needed.
- a1369209993 7y agoAssuming the keys are Hash + Ord, wouldn't that just be: for (k,v) in &src { dst.insert(k,v); } ?
- StavrosK 7y agoYes, that's what I ended up doing, but the error confused me and I ended up looking for a less hacky way for more than an hour until I said "fuck it" and just did that.
- CDSlice 7y agoIf you are using dictionary in the Python sense, Rust has that built in too, you just have to import it from the standard library collections module. https://doc.rust-lang.org/std/collections/struct.HashMap.html https://doc.rust-lang.org/std/collections/struct.HashMap.htm...
- jonesetc 7y agoI know it's not the whole point, but there is a hash map and sorted (btree) map in the std collections. That being said the point still stands, I remember specifically feeling it at the lack of included regex. I overall prefer the slim std lib of rust over the massive python though. Especially with the community being pretty good about nominating de facto standard packages like for regex.