Hey Phil, That's precisely why I opened the following PR: https://github.com/mozilla/rust/pull/12960
But as Daniel and Alex pointed out, it is pretty redundant. Alex also pointed out that the LLVM optimization pass will optimize away the return value. Scala also allows you to use `for` to deal with options. This is especially useful when you have multiple optional values, and you want them to all be `Some`s to proceed. E.g. val x = Some(3) val y = Some(4) val sum = for { a <- x b <- y } yield (a + b) // sum is now Some(7) The word is that hopefully Rust will gain this capability fully when HKTs are implemented (possibly using the `do` keyword). Right now there are a couple of macro implementations that mimic this feature, and they were posted on the mailing list. Cheers -- Ziad On Tue, Mar 18, 2014 at 1:56 PM, Phil Dawes <rustp...@phildawes.net> wrote: > Hello! > > I had my first github patch yesterday - Hatahet kindly cleaned up some > code for me. I was using match to destructure the Option<uint> output from > str.find_str(), but then doing nothing with the None case. He wrote: > > 'The match expression for options is good for when you want to deal with > both cases of the Option type -- namely Some and None. If you only deal > with Some, then you can cut down on some redundancy by using Option.map() > or Option.iter().' > > https://github.com/phildawes/racer/pull/1 > > so all the instances of > > match line.find_str(..) { > Some(n) => { > ... > } > None => {} > } > > became: > > for n in line.find_str(..).move_iter() { > ... > } > > Which is much more consise and so I applied the patch. > > However now reading it again I find using 'for' in this way a bit > confusing because it says to me that there are potentually multiple results > to the find_str() method (implying that it finds multiple matches of the > string when really it does not). > > Using map looks is less confusing to my eyes, but it feels hacky because > I'm discarding the result and I guess is still imply iteration over > multiple results: > > line.find_str(..).map(|n|{ > ... > }); > > Is there a clearer way? > > Thanks, > > Phil > > > _______________________________________________ > Rust-dev mailing list > Rust-dev@mozilla.org > https://mail.mozilla.org/listinfo/rust-dev > >
_______________________________________________ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev