What are consume, borrowing, and noncopyable types, at a high level?

1.8.5 · L5 · Learning

Predict first

Does this compile?

struct Token: ~Copyable {}

let a = Token()
let b = a
let c = a
  • Yes
  • No

Mental model. A noncopyable value is a physical key. You can hand it over or let someone look at it, but you cannot duplicate it.

Ownership annotations express move vs read: consume ends a value's life (it is moved, not copied); borrowing grants read access without a copy. ~Copyable types opt out of implicit copying entirely.

They matter for resource types (file handles, unique buffers) and for high-performance stdlib internals. Most app code still copies structs and never notices.

A noncopyable value is moved on assignment. After let b = a, a is consumed and cannot be used again.

Common traps

  • Thinking this is only for stdlib authors. It already affects Span-style APIs and will show up in more framework signatures.
  • “consume is like move in C++.” Similar spirit, but Swift's rules are enforced at the call site with different diagnostics.

They might follow up with

  • What problem does it solve? Implicit copies of large or unique resources — both a correctness and a performance problem.
  • Can a noncopyable type conform to protocols? Yes, though generic constraints need ~Copyable support, which is still rolling out.

Related questions

  • 1.1.3 Where do value types live — stack or heap?
  • 1.5.3 Why are closures called reference types? What is a closure context?

← What does defer guarantee? What is the order of multiple defers?