# Random Musings: types in patterns

**URL:** <https://internals.rust-lang.org/t/random-musings-types-in-patterns/7316>\
**Category:** language design\
**Created:** [April 16, 2018, 4:52am UTC](https://internals.rust-lang.org/t/random-musings-types-in-patterns/7316 "2018-04-16T04:52:58Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![petrochenkov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/petrochenkov/32/2784_2.png) [@petrochenkov](https://internals.rust-lang.org/u/petrochenkov)\
**Post date:** [April 16, 2018, 9:58am UTC](https://internals.rust-lang.org/t/random-musings-types-in-patterns/7316/6 "2018-04-16T09:58:44Z")

</div>

> [@scottmcm](#):
>
> The notion of types-in-patterns

There's an old RFC issue about this:

> <https://github.com/rust-lang/rfcs/issues/354>
>
> Post-1.0, we would like to allow arbitrary type ascription - that is annotating …any expression with a type. E.g., \`let \_ = foo(x, y: Bar\<int\>, z);\` (type ascription on the sub-expression \`y\`).
> 
> Detail to be nailed down - precedence (probably the same as \`as\`).
> 
> Optional extra - type ascription on patterns, e.g. \`let (x: Bar\<int\>, y) = foo(...);\` - useful when you care about the type of some part of the pattern but not others, especially when the bits you don't care about are \`\_\`.
> 
> Optional, optional extra - using the ascribed types in pattern matching for downcasting (an extension of some #349 proposals).

It should be relatively easily implementable and I think we should implement it as an experiment under `type_ascription` feature ([Idea: Change type ascription syntax from `:` to `type` keyword - #4 by petrochenkov](https://internals.rust-lang.org/t/idea-change-type-ascription-syntax-from-to-type-keyword/6666/4)).

Also, there's no ambiguity in struct patterns, only confusion.  
`field` in `Struct { field }` is NOT a pattern, so type ascription is not applicable to it.  
The syntax is either `Struct { ident: PAT }` (in this case there's a pattern and this pattern can use type ascription) or `Struct { ident }` (in this case there's no pattern syntactically, so there can be no type ascription).

---

_[View the full topic](https://internals.rust-lang.org/t/random-musings-types-in-patterns/7316)._
