SwiftQuiz
#16

Conditional Consume

  • noncopyable
  • ownership
Swift 6.3.2 Report an issue

Verified quizzes have their expected answer checked by compiling and running the code with the listed Swift toolchain during the site build.

struct File: ~Copyable {
    deinit {
        print("deinit")
    }

    borrowing func write(_: [UInt8]) {}
    consuming func close() {}
}

let condition = false

do {
    let file = File()
    file.write([1, 2, 3])

    if condition {
        file.close()
    } else {
        print("not closed")
    }

    if !condition {
        file.write([1])
        file.close()
    }

    print("done writing")
}
Hint

What's the lifecycle of file?

Your answer

What does this program do?