Menu
CoddyTech

Working With Files

Lesson 1 of 48 in Coddy's Swift for Rust Developers course.

Rust organizes a crate through module declarations and a main function, while these Swift programs compile sibling source files together as one module. The supplied main.swift holds top-level executable statements, free functions live in Functions.swift and later named types use separate files. Import Foundation in each file needing its APIs rather than importing sibling Swift files. Run the complete first task and inspect the driver call to welcome(_:). Later exercises preserve exact Swift APIs while exploring values, references, optionals, protocols and ownership differences; the locked driver handles input and printing.

challenge icon

Challenge

Easy

Run the supplied program. Input is one ASCII name of 1 to 20 letters. welcome(_:) returns Hello, followed by that name. The driver prints it. All Swift files compile together; top-level statements belong only in main.swift. Unlike Rust, Swift uses this filename for the executable entry point. Free functions live in Functions.swift; later named types each have their own file.

Implement the required APIs and solve(_ input: String) -> String. Return output text without a final newline; do not print in editable code. The locked driver independently checks the APIs on additional valid inputs, then prints the returned text with exactly one newline. All numeric operations stay within Int bounds.

Your code goes in Functions.swift. main.swift holds the supplied input/output code and cannot be edited.

Try it yourself

import Foundation
func apiChecks(_ input: String) -> Bool {
return welcome("Ada") == "Hello, Ada"
}
let input = String(data: FileHandle.standardInput.readDataToEndOfFile(), encoding: .utf8)!
// BEGIN PUBLIC API PROBES
let apiOK = apiChecks(input)
// END PUBLIC API PROBES
if apiOK { print(solve(input)) } else { print("API mismatch") }

All lessons in Swift for Rust Developers

Practice on your own: Swift playground