Question: I ran into this code which is part of a Swift implementation of a linked list in the [Swift Algorithm Club](https://github.com/raywenderlich/swift-algorithm-club/tree/master/Linked%20List). Throughout the implementation the author uses `case let` following a while statement right before unwrapping an optional. I've never seen the case keyword used outside of the context of a switch statement, and I'm wondering what exactly it does? Does it somehow cast the `let next? = node.next` part to true or false, maybe depending on whether `next?` becomes nil or not?

```
public var last: Node? {
    if var node = head {
        while case let next? = node.next {
            node = next
        }
        return node
    } else {
        return nil
    }
}

```

Answer: 
This is the [Optional Pattern](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Patterns.html#//apple_ref/doc/uid/TP40014097-CH36-ID520). It tests and unwraps an `Optional`, executing the conditional only if the `Optional` is non-nil.

The keyword `case` is needed because it follows from the original `switch`...`case` syntax. The `case` tests a pattern and if it matches then the following statement is executed. In your example the `let next?` is the pattern. If the value is unwrapped and assigned then the `case` matches and your code is executed.

From the documentation:

> 
> **Optional Pattern**
> 
> 
> An optional pattern matches values wrapped in a Some(Wrapped) case of
>  an Optional or ImplicitlyUnwrappedOptional
>  enumeration. Optional patterns consist of an identifier pattern
>  followed immediately by a question mark and appear in the same places
>  as enumeration case patterns.
> 
> 
> Because optional patterns are syntactic sugar for Optional and
>  ImplicitlyUnwrappedOptional enumeration case patterns, the following
>  are equivalent:
> 
> 
> 
> ```
> let someOptional: Int? = 42
> // Match using an enumeration case pattern
> if case .Some(let x) = someOptional {
>     print(x)
> }
> 
> // Match using an optional pattern
> if case let x? = someOptional {
>     print(x)
> }
> 
> ```
> 
> The optional pattern provides a convenient way to iterate over an
>  array of optional values in a for-in statement, executing the body of
>  the loop only for non-nil elements.
> 
> 
> 
> ```
> let arrayOfOptionalInts: [Int?] = [nil, 2, 3, nil, 5]
> // Match only non-nil values
> for case let number? in arrayOfOptionalInts {
>     print("Found a \(number)")
> }
> // Found a 2
> // Found a 3
> // Found a 5
> 
> ```
> 
>
Swift 2 took the pattern paradigm from `switch/case` statements and allowed it to be used in other contexts (`if`, `while` and so on).

So now, rather than just simple comparisons, you can use these pattern matching comparisons in conditionals as well.

As one example, rather than:

```
if (a >= 0) and (a <= 255)

```

you can instead use:

```
if case 0...255 = a

```

That's a trivial example but it can become much more useful once you realise the rather large number of pattern matching options available to you.