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 (?) Cheers, Stefan _______________________________________________ Rust-dev mailing list [email protected] https://mail.mozilla.org/listinfo/rust-dev
