Stream: general

Topic: match on Option


RalfJ (Nov 25 2018 at 14:59, on Zulip):

Is there a good way to match on an Option<String>, with string literals?

RalfJ (Nov 25 2018 at 14:59, on Zulip):

for String there is as_ref, but that does not work here

RalfJ (Nov 25 2018 at 15:00, on Zulip):

ah, .as_ref().map(|s| &**s) works

centril (Nov 25 2018 at 15:00, on Zulip):

yeah tho that is exceedingly fugly

RalfJ (Nov 25 2018 at 15:01, on Zulip):

yeah

centril (Nov 25 2018 at 15:01, on Zulip):

@RalfJ try .deref() ?

RalfJ (Nov 25 2018 at 15:02, on Zulip):

but thats not implemented for Option?

centril (Nov 25 2018 at 15:02, on Zulip):

@RalfJ https://doc.rust-lang.org/nightly/std/option/enum.Option.html#method.deref

RalfJ (Nov 25 2018 at 15:04, on Zulip):

@Mazdak Farrokhzad ah! thanks. I had no idea.

RalfJ (Nov 25 2018 at 15:04, on Zulip):

the name is odd though

RalfJ (Nov 25 2018 at 15:05, on Zulip):

I am taking a reference, not dereferencing one

centril (Nov 25 2018 at 15:05, on Zulip):

@RalfJ hehe kinda; it's a bit hacky and recent

centril (Nov 25 2018 at 15:05, on Zulip):

but nice

centril (Nov 25 2018 at 15:06, on Zulip):

@RalfJ maybe it should be called .map_ref() ?

centril (Nov 25 2018 at 15:07, on Zulip):

ostensibly it is consistent with the .as_ref() method on Option tho

RalfJ (Nov 25 2018 at 15:07, on Zulip):

as ref always returns an optional ref, that makes sense

centril (Nov 25 2018 at 15:07, on Zulip):

ye

nagisa (Nov 25 2018 at 15:09, on Zulip):

@RalfJ that method does exactly what Deref::deref does.

nagisa (Nov 25 2018 at 15:10, on Zulip):

within an option, that is.

nagisa (Nov 25 2018 at 15:10, on Zulip):

(<String as Deref>::deref(x: &String) -> &str)

centril (Nov 25 2018 at 15:11, on Zulip):

it's fmap deref sorta :P

RalfJ (Nov 25 2018 at 15:11, on Zulip):

it's fmap deref sorta :P

plus auto-ref^^

Alexander Regueiro (Nov 25 2018 at 18:19, on Zulip):

@Mazdak Farrokhzad ohai.

Alexander Regueiro (Nov 25 2018 at 18:19, on Zulip):

welcome to Zulip.

centril (Nov 25 2018 at 18:44, on Zulip):

ohai!

Nicole Mazzuca (Nov 26 2018 at 01:29, on Zulip):

@RalfJ something like generalized ref/box patterns would help

Nicole Mazzuca (Nov 26 2018 at 01:29, on Zulip):

i.e., match opt_string { Some(&"Hello!") => ..., None => ... }

RalfJ (Nov 26 2018 at 11:07, on Zulip):

yeah... though the syntax looks a bit odd here, usually pattern syntax matches construction syntax, but Some(&"hello") is not how you construct an Option<String>

RalfJ (Nov 26 2018 at 11:07, on Zulip):

Some(box "hello") would make a bit more sense

Nicole Mazzuca (Nov 26 2018 at 18:38, on Zulip):

Some (box "hello") would imply ownership, i.e., DerefOwn

Nicole Mazzuca (Nov 26 2018 at 18:38, on Zulip):

you probably want Deref

rkruppe (Nov 26 2018 at 18:41, on Zulip):

In analogy with box expressions constructing arbitrary smart pointers (e.g. Rc) I would intuitively prefer if this syntax simply "matches on the pointee" and if the nested pattern moves out then that is only valid if you can move out of that smart pointer type. It's true that box isn't a great keyword for that because of the similarity with Box, but the same issue applies to box EXPR so it's not pattern-specific.

rkruppe (Nov 26 2018 at 18:43, on Zulip):

oh, ugh, box-for-non-Box was never implemented

Nicole Mazzuca (Nov 26 2018 at 18:43, on Zulip):

I would prefer something like &<pat> does a Deref, &mut <pat> does a DerefMut, and box <pat> does a DerefOwn.

RalfJ (Nov 26 2018 at 21:22, on Zulip):

Some (box "hello") would imply ownership, i.e., DerefOwn

well, I am matching a String which is owned like a box

Nicole Mazzuca (Nov 26 2018 at 21:32, on Zulip):

right, but I would expect that to move _out_ of the string.

rkruppe (Nov 26 2018 at 21:36, on Zulip):

why? even if you view box patterns as solely about DerefOwned, afaik every other pattern that can move out doesn't have to, e.g. Some(_) doesn't move out of an Option while Some(x) does (default binding modes nonwithstanding)

rkruppe (Nov 26 2018 at 21:37, on Zulip):

or is this because to match you first have to call DerefOwned and then you can't put it back?

Nicole Mazzuca (Nov 26 2018 at 21:37, on Zulip):

the latter

Nicole Mazzuca (Nov 26 2018 at 21:37, on Zulip):

and also, there are types which you want to match on as a reference which do not own their contents.

RalfJ (Nov 26 2018 at 21:49, on Zulip):

the thing is, "hello" already is a reference

RalfJ (Nov 26 2018 at 21:50, on Zulip):

&"hello" should match an &&str...

Nicole Mazzuca (Nov 27 2018 at 01:45, on Zulip):

Yes, and?

Nicole Mazzuca (Nov 27 2018 at 01:45, on Zulip):

generalized reference patterns :P

centril (Nov 27 2018 at 10:10, on Zulip):

generalize all the things :P

Last update: Nov 20 2019 at 11:25UTC