Menu
CoddyTech

Numbers and Casts

Lesson 5 of 48 in Coddy's Rust for C++ Developers course.

C++ usual arithmetic conversions can change operand types before an operation. Rust requires compatible numeric types and uses explicit conversions where needed. Integer division truncates toward zero in both languages for the signed values shown here, so the remainder of a negative dividend can be negative. Converting an already-computed integer quotient to floating point cannot recover its fractional part. Convert an operand before division when a fractional result is required. Rust as is a numeric cast, not a general replacement for C++ conversion constructors or dynamic_cast. Widening must happen before multiplication when the intermediate result needs the larger range. Do not infer overflow behavior from these small examples: C++ signed overflow is undefined, while Rust's overflow checks depend on compilation settings.

Complete standalone C++17 program:

#include <iomanip>
#include <iostream>
int main() {
    int a = 7, b = 2;
    std::cout << std::fixed << std::setprecision(1);
    std::cout << static_cast<double>(a / b) << ' ' << static_cast<double>(a) / b << '\n';
}

Complete standalone Rust program:

fn main() {
    let samples: i32 = 7;
    let groups: i32 = 2;
    println!("{} {:.1}", samples / groups, samples as f64 / groups as f64);
    println!("{} {}", -7 / 2, -7 % 2);
}
challenge icon

Challenge

Easy

Split elapsed seconds. Input is one unsigned integer from 0 through 1000000. Required API: pub fn whole_minutes(seconds: u32) -> u32 returns the number of complete minutes; pub fn leftover_seconds(seconds: u32) -> u32 returns the remainder from 0 through 59; pub fn milliseconds(seconds: u32) -> u64 widens the argument and multiplies it by 1000. Output minutes:remainder:milliseconds with no spaces. The APIs also accept any u32, including values beyond the input bound. Implement the public API in duration.rs. The supplied exercise.rs parses valid input and calls that API. Return the specified text without a trailing newline; the locked driver adds one newline. Do not print from the API. All input values satisfy the stated bounds.

Implement the required exported APIs and return the result from exercise::solve(input: &str) -> String without a final newline. Do not print in editable code. The locked driver checks required APIs independently, reads all stdin, and prints the returned text with one final newline.

Your code goes in exercise.rs, duration.rs. main.rs holds the supplied input/output code and cannot be edited.

Try it yourself

#![allow(dead_code, unused_imports, unused_variables, unused_mut)]
mod exercise;
mod duration;
use std::io::{self, Read};
fn main() {
    let mut input=String::new();
    io::stdin().read_to_string(&mut input).unwrap();
    // BEGIN PUBLIC API PROBES
assert_eq!(duration::whole_minutes(183), 3);
assert_eq!(duration::leftover_seconds(183), 3);
assert_eq!(duration::milliseconds(183), 183000u64);
assert_eq!(duration::whole_minutes(0), 0);
assert_eq!(duration::leftover_seconds(0), 0);
assert_eq!(duration::milliseconds(0), 0);
assert_eq!(duration::milliseconds(4000000000), 4000000000000u64);
assert_eq!(duration::whole_minutes(183), 3);
    // END PUBLIC API PROBES
    println!("{}", exercise::solve(&input));
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Rust for C++ Developers

Practice on your own: Online Rust compiler