Menu
CoddyTech

Recap: Storage Invoice

Lesson 6 of 48 in Coddy's Rust for Go Developers course.

challenge icon

Challenge

Easy

Invoice reserved storage. Input contains blocks rate fee, three unsigned integers no greater than 1000000. Required API: pub fn storage_cost(blocks: u32, rate: u32) -> u64 returns their product without 32-bit overflow; pub fn invoice(blocks: u32, rate: u32, fee: u32) -> u64 adds the one-time fee to that product. Output storage=cost and total=invoice on two lines. A zero block count still incurs the one-time fee. Implement the public API in billing.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, billing.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 billing;
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!(billing::storage_cost(321, 17), 5457);
assert_eq!(billing::invoice(321, 17, 29), 5486);
assert_eq!(billing::storage_cost(0, 31), 0);
assert_eq!(billing::invoice(0, 31, 11), 11);
assert_eq!(billing::storage_cost(900000, 800000), 720000000000u64);
assert_eq!(billing::invoice(321, 17, 29), 5486);
    // END PUBLIC API PROBES
    println!("{}", exercise::solve(&input));
}

All lessons in Rust for Go Developers

Practice on your own: Online Rust compiler