Rust Fundamentals · 10 XP

Constants and shadowing

A constant is a value fixed at compile time. Declare it with const, give it an explicit type, and name it in SCREAMING_SNAKE_CASE. Unlike let, constants can live outside functions and can never be mut.

const MAX_PLAYERS: u32 = 4;

Shadowing means declaring a new variable with the same name using let. The old one is hidden from then on. This differs from mut: you create a brand new variable, so it may even have a different type.

let input = "  42  ";
let input = input.trim();   // still text, but trimmed
let input = input.len();    // now a number

trim() removes whitespace at both ends of a piece of text, and len() gives its length in bytes.

Your task

  1. Set the two constants: SECONDS_PER_MINUTE is 60, and SECONDS_PER_HOUR must be computed from it (60 minutes' worth of seconds).
  2. Finish total_seconds(hours: u32, minutes: u32) -> u32 using the constants.
  3. Finish trimmed_len(text: &str) -> usize. Shadow text first with the trimmed text, then shadow it again with its length, and return that.

Next lesson →

Sign in to run your code. It's free.