The notion of types-in-patterns came up twice, afaict independently, within 8 hours of each other on Saturday:
- IRC https://botbot.me/mozilla/rust-lang/2018-04-14/?msg=98978699&page=1 (cc @varkor)
- RFCs https://github.com/rust-lang/rfcs/pull/2388#issuecomment-381300774
(I don’t actually intend to propose anything right now; just thought it an interesting area to explore.)
For example, I rather like this:
match "4".parse() {
Ok(x: u16) => ...,
Err(e) => ...,
}
Compared to ::<u16>
or let r: Result<u16, _> =
, that feels more direct and less noisy/sigily.
Interestingly, one can already do a hacky version of that that if parsing a struct:
match "asdf".parse() {
Ok(x @ String {..}) => println!("yup: {}", x),
Err(e) => eprintln!("Nope: {:?}", e),
}
I could also imagine something like this being legal:
fn foo(Wrapping(x: u16)) -> ...
since it fully-specifies the type of the function argument.
Of course, @hanna-kruppe rightly pointed out that this mixes poorly with struct
patterns, since :
there already has a different meaning.