On Tue, Dec 17, 2013 at 2:06 PM, Stefan Plantikow <[email protected]> wrote: > Hi, > > > Am 09.12.2013 um 16:53 schrieb Damien Radtke <[email protected]>: > >> I have no idea if it would be feasible in the standard library, but wouldn't >> the ideal solution be having one function (e.g. from_utf8()) that could >> return two possible values, a bare result and an Option? Letting the >> compiler decide which version to use based on type inference like this: >> >> let result: ~str = from_utf8(...); >> let result: Option<~str> = from_utf8(...); >> >> Assuming both of them are passed invalid UTF8, then the first version would >> fail, but the second version would just return None. > > > If rust allowed pattern matching in let, this problem would go away: > > let Some(~result) = from_utf8(…); > > This would fail if from_utf8(..) would not return None. This solution has > several benefits: > > - The code clearly expresses intent (a result was expected and thus it should > fail if instead None is returned) > - Only one function is needed and that functions signature covers all > possible return cases > - This approach isn’t limited to Option and could very well also work with > Result. The generated error message could contain the value that was not > matched (and thus have more detailed error information). > - IMHO the required syntax change won't break existing code (?) >
We already have pattern matching in `let` (the LHS is a pattern), but it's only for irrefutable patterns. IOW, `let` can never fail, and that's a very very useful property IMO. _______________________________________________ Rust-dev mailing list [email protected] https://mail.mozilla.org/listinfo/rust-dev
