Open
Description
I've written a lot of code like this for libraries that use Iterators where the Item is a Result:
for line_r in stdin.lock().lines() {
let line = line_r?;
// do something with line
}
I think it would be awesome if we could just relocate the ?
to the pattern, like this:
for line? in stdin.lock().lines() {
// do something with line
}
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
burdges commentedon Jun 1, 2017
You can do this using
while let
already, so you should be asking forfor let
which seemingly does not appear in thewhile let
RFC.burdges commentedon Jun 1, 2017
Oh you actually want an
?
type error to propagate if that part does not unwrap. That makes sense. I'd still think you'd need an actual pattern match first, meaningfor let
.eddyb commentedon Jun 1, 2017
@burdges What do you mean?
for
takes a pattern, e.g.for &(a, b) in slice_of_pairs
is valid.burdges commentedon Jun 1, 2017
I see,
for
needs an irrefutable pattern, which thispat?
proposal is. There is no need forfor let
as just sugar forlet _iter = ..; while let Some(pat) = _iter.next() { .. }
.devyn commentedon Jun 1, 2017
Yeah, exactly, I think it would make sense just as a general pattern too:
would be allowed to be equivalent to:
mgattozzi commentedon Jun 1, 2017
That seems a bit confusing. It makes it seem like the
?
inlet foo?
is part of the identifier here. I think it would make more sense to have it like this in your original example:jD91mZM2 commentedon Jun 1, 2017
Doing
?
at the end like in @mgattozzi probably isn't a good idea, since that's currently how you would unwrap the return value fromlines()
- not each item.mgattozzi commentedon Jun 1, 2017
Right. I wasn't clear, I should have said an iterator after in with a question mark would do the unwrapping of the value returned while iterated through.
lines
by itself doesn't return a result so this wouldn't work as is today anyways.eddyb commentedon Jun 1, 2017
The only syntax I could see being added, that's not in the pattern, is
for x in? xs
.jD91mZM2 commentedon Jun 1, 2017
for? x? in? xs??!?!?!?!
EDIT: Actually, that isn't an absolutely horrible idea. Maybe we could have like
for line in stdin.lock().lines()?!
... But yeah, probably not.burdges commentedon Jun 1, 2017
I think it should extend pattern matching in general, not be special syntax for
for
.Ideas around coroutines, futures, etc. could perhaps be used to generalize
for
to support early return, so maybe :I despise that idea though because if
for
loops can return early then a crate changing a type could wreak havoc in the logic of its users. I guess that's why folks are discussingfor? .. in? ..
etc.As an aside, I'd write the original code to avoid introducing another name. I might insert context not allowed by
From<io:Result>
too. It looks more reasonable then :I'm dubious about incorporating
map_err
like that :Stebalien commentedon Jun 1, 2017
Previous discussion: https://internals.rust-lang.org/t/pre-rfc-allow-in-patterns/4772
nixpulvis commentedon Jun 29, 2017
I'm still biased against the addition of the
?
operator in general... That said I'd strongly avoid putting it near the identifier, seeing as it would read more like "this thing IS an option" instead of "this thing unwraps an option".joshtriplett commentedon Jul 27, 2017
I've wanted something like this for a while, and I'd like to see some reasonable syntax for it.
?
operator infor
loops rust-lang/rust#44170Centril commentedon Apr 26, 2018
Triage ping: Any updates on this?
scottmcm commentedon Apr 26, 2018
That for loop example is compelling, but having this in the pattern scares me.
It reminds me too much of
ref
and&
, and the confusion caused by having bothSo I wouldn't want to end up with debates about which of the following to use:
Not that I have a good solution to propose...
glaebhoerl commentedon Apr 26, 2018
let try
🙃