Stream: general

Topic: HashMap::remove return value


Daniel Papp (Jun 12 2019 at 09:36, on Zulip):

Does a Rust's Hashmap own the actual value and when I remove it from the HashMap I get an Option<V> because the ownership is getting transferred?

oli (Jun 12 2019 at 09:42, on Zulip):

yes

oli (Jun 12 2019 at 09:42, on Zulip):

Same thing with a Vec. When you pop the last elemen, you get ownership back

Daniel Papp (Jun 12 2019 at 10:25, on Zulip):

Thanks!

RalfJ (Jun 12 2019 at 11:00, on Zulip):

speaking of which, does someone know a nice and readable way to assert that we got a None back?
I dislike assert!(foo.remove(...).is_none()); because usually one expects the body of an assert! to not have important side-effects.

Jake Goulding (Jun 12 2019 at 15:04, on Zulip):

The dumb answer is to move the side-effect out of the assert; is that good enough?

I've been bitten by putting important-to-the-functionality code in asserts in C and wondering why the production code fails, but at least in Rust you have to do out of your way to do that with debug_assert

Jake Goulding (Jun 12 2019 at 15:04, on Zulip):

Depending on what you are testing, you could just do assert(foo.is_empty())

Jake Goulding (Jun 12 2019 at 15:05, on Zulip):

And I think the context is relevant. I have no problems with side effects in an assert in a test.

rkruppe (Jun 12 2019 at 15:18, on Zulip):

I don't understand why one would write assert!(foo.remove(...).is_none()). If it returns None and the assertion passes, then the map didn't contain the key to begin with and nothing was removed, so why can't this be written as assert!(!foo.contains(...));?

oli (Jun 12 2019 at 15:20, on Zulip):

@rkruppe I think the original example was meant to be HashMap::insert

rkruppe (Jun 12 2019 at 15:21, on Zulip):

Ah, that makes sense

Jake Goulding (Jun 12 2019 at 15:27, on Zulip):

heh, you could do assert!(map.insert(the_key_again).is_some()) to be really confusing

oli (Jun 12 2019 at 15:32, on Zulip):

Now I want a clippy lint against that because I've never seen it used and thus have concluded via anecdotal evidence that the pattern is probably a bug and the user meant .is_none()

oli (Jun 12 2019 at 15:32, on Zulip):

actually

oli (Jun 12 2019 at 15:32, on Zulip):

the user should just have done map[key] = value; instead

Jake Goulding (Jun 12 2019 at 15:33, on Zulip):

You can't do that :-)

Jake Goulding (Jun 12 2019 at 15:33, on Zulip):

How can I update a value in a mutable HashMap?

oli (Jun 12 2019 at 15:34, on Zulip):

damn

Jake Goulding (Jun 12 2019 at 15:34, on Zulip):

@oli tangentially, I saw some SO comments from you recently back when you started Rust. It's really fun seeing the delta to today :-)

oli (Jun 12 2019 at 15:34, on Zulip):

hahah

oli (Jun 12 2019 at 15:34, on Zulip):

yea

oli (Jun 12 2019 at 15:35, on Zulip):

it's probably visible just in my year of SO

RalfJ (Jun 12 2019 at 16:02, on Zulip):

yeah sorry I meant insert. and I meant in real code, not tests, so side-effects in the assert! are bad.

RalfJ (Jun 12 2019 at 16:02, on Zulip):

I also dont want to spend two lines on this

RalfJ (Jun 12 2019 at 16:03, on Zulip):

for the other case ("assert is_some"), we have unwrap. So what is missing is a method that panics on some that can be nicely chained.

RalfJ (Jun 12 2019 at 16:03, on Zulip):

or alternatively, a method on bool to assert it is true or so...
map.insert(...).is_none().assert() ;)

Laurențiu Nicola (Jun 12 2019 at 16:49, on Zulip):

ahem map.insert(...).is_none().assert!()

Jake Goulding (Jun 12 2019 at 16:53, on Zulip):

Gonna drag the postfix macros bikeshed into this faffing about, eh

Jake Goulding (Jun 12 2019 at 16:54, on Zulip):

So you want Option::unwrap_none; in parallel to Result::unwrap_err, almost.

Jake Goulding (Jun 12 2019 at 17:00, on Zulip):

@RalfJ who puts asserts in the production code? Don't you trust yourself :wink:

RalfJ (Jun 12 2019 at 17:02, on Zulip):

"Beware of bugs in my code. I have only proven it correct, not tested it."

RalfJ (Jun 12 2019 at 17:02, on Zulip):

So you want Option::unwrap_none; in parallel to Result::unwrap_err, almost.

yes

Last update: Nov 21 2019 at 23:50UTC