# Closure with explicit vs implicit capturing

**URL:** <https://forums.swift.org/t/closure-with-explicit-vs-implicit-capturing/52776>\
**Category:** Using Swift\
**Tags:** closure\
**Created:** [October 13, 2021, 10:04am UTC](https://forums.swift.org/t/closure-with-explicit-vs-implicit-capturing/52776 "2021-10-13T10:04:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkbrwr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mkbrwr/32/15835_2.png) [@mkbrwr](https://forums.swift.org/u/mkbrwr)\
**Post date:** [October 13, 2021, 10:04am UTC](https://forums.swift.org/t/closure-with-explicit-vs-implicit-capturing/52776/1 "2021-10-13T10:04:47Z")

</div>

Encountered the next code

var thing = "cars"

let closure1 = { [thing] in  
print("I love (thing)")  
}

let closure2 = {  
print("I love (thing)")  
}

thing = "airplanes"

closure1() // I love cars  
closure2() // I love airplanes

Why is the behaviour different for explicit and implicit capturing ?

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [October 13, 2021, 10:53am UTC](https://forums.swift.org/t/closure-with-explicit-vs-implicit-capturing/52776/2 "2021-10-13T10:53:58Z")

</div>

> [@](#):
>
> [The Swift Programming Language: Redirect](https://docs.swift.org/swift-book/ReferenceManual/Expressions.html#ID544)  
> Capture Lists  
> ...  
> The entries in the capture list are initialized when the closure is created. For each entry in the capture list, a constant is initialized to the value of the constant or variable that has the same name in the surrounding scope. For example in the code below, `a` is included in the capture list but `b` is not, which gives them different behavior.
> 
> ```
> var a = 0
> var b = 0
> let closure = { [a] in
> print(a, b)
> }
> 
> a = 10
> b = 10
> closure()
> // Prints "0 10"
> 
> ```
> 
> There are two different things named `a` , the variable in the surrounding scope and the constant in the closure’s scope, but only one variable named `b` . The `a` in the inner scope is initialized with the value of the `a` in the outer scope when the closure is created, but their values aren’t connected in any special way. This means that a change to the value of `a` in the outer scope doesn’t affect the value of `a` in the inner scope, ~~nor does a change to `a` inside the closure affect the value of `a` outside the closure~~. In contrast, there’s only one variable named `b` —the `b` in the outer scope—so changes from inside or outside the closure are visible in both places.

---

<div class="post-metadata">

**Author:** ![mkbrwr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mkbrwr/32/15835_2.png) [@mkbrwr](https://forums.swift.org/u/mkbrwr)\
**Post date:** [October 13, 2021, 11:05am UTC](https://forums.swift.org/t/closure-with-explicit-vs-implicit-capturing/52776/3 "2021-10-13T11:05:15Z")

</div>

Turns out there is document that answers my question directly, thanks for pointing it out. Now it's clear!
