Skip to content

borrowing & consuming — Parameter Ownership

Purpose: Explicit ownership modifiers for performance optimization and noncopyable type support Swift Version: Swift 5.9+ iOS Version: iOS 13+

When to Use This Skill

Use this skill when:

  • Large value types being passed read-only (avoid copies)
  • Working with noncopyable types (~Copyable)
  • Reducing ARC retain/release traffic
  • Factory methods that consume builder objects
  • Performance-critical code where copies show in profiling

Do NOT use this skill for:

  • Simple types (Int, Bool, small structs)
  • Compiler optimization is sufficient (most cases)
  • You're not certain about the performance impact

Quick Reference

ModifierOwnershipCopiesUse Case
(default)Compiler choosesImplicitMost cases
borrowingCaller keepsExplicit copy onlyRead-only, large types
consumingCaller transfersNone neededFinal use, factories
inoutCaller keeps, mutableNoneModify in place

Key Patterns

Read-Only Large Struct

swift
// ✅ Explicit borrow — no copy
func process(_ buffer: borrowing LargeBuffer) -> Int {
    buffer.data.count
}

Consuming Factory

swift
struct Builder {
    consuming func build() -> Product {
        Product(config: config)
    }
}

let product = builder.build()
// builder is now invalid

Noncopyable Type

swift
struct FileHandle: ~Copyable {
    borrowing func read(count: Int) -> Data { /* ... */ }
    consuming func close() { /* ... */ }
}

Resources

Released under the MIT License