Numbers and Conversion
Lesson 3 of 48 in Coddy's Swift for Rust Developers course.
Rust and Swift both distinguish integer and floating-point types, and integer division truncates toward zero. Rust makes widths explicit through types such as i32 and i64; Swift Int is platform-sized and 64-bit on this runtime. Conversion before division preserves a fractional quotient, while converting an already-truncated result does not. Rust integer overflow behavior depends on enabled overflow checks and the chosen operation; use checked or wrapping methods when that policy matters. Ordinary Swift Int overflow traps unless an explicit alternative operation is chosen. The bounded example avoids overflow and produces three followed by three and a half.
Rust
fn main() {
let a: i64 = 7;
let b: i64 = 2;
println!("{}", a / b);
println!("{}", a as f64 / b as f64);
}
Swift
let a = 7
let b = 2
print(a / b)
print(Double(a) / Double(b))Challenge
EasyImplement clockParts(_ minutes: Int) -> String. Input is total minutes, 0 through 10000. Return whole hours, a colon, then remaining minutes, without zero padding.
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 clockParts(181) == "3:1" && clockParts(60) == "1:0"
}
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") }
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Swift for Rust Developers
1Starting Swift
Working With FilesBindings and MutationNumbers and ConversionBoolean ConditionsRecap: Fare Quote4Functions and Arrays
Functions and LabelsInout and Exclusive AccessTuples and Multiple ResultsArrays Have Value SemanticsRecap: Rising Steps2Text and Decisions
Strings and InterpolationOptionals and BindingGuard and Early ExitIf and Branch ValuesRecap: Entry RulesPractice on your own: Swift playground