Start learning free

LESSON 219 OF 293 · WHILE LOOPS - CHALLENGES

Number of Digits in Rust: Counting with a while Loop

Practice Read the full explanation

Given a non-negative integer n, return how many digits it has.

The digits are the figures from 0 to 9 that n is written with. Dividing a number by 10 without the remainder removes its last digit (542 becomes 54), so you can count how many times that happens until the number is 0. The number 0 itself has one digit.

 

Example 1:

Input: n = 542
Output: 3
Explanation: 542 is written with three digits: 5, 4 and 2.

Example 2:

Input: n = 100
Output: 3
Explanation: The zeros are digits too: 1, 0 and 0.

Example 3:

Input: n = 0
Output: 1
Explanation: 0 is written with one digit.

 

Constraints:

  • 0 <= n <= 109
STEP 1 OF 7
    Ctrl + Enter to check

    RUST · WHILE LOOPS - CHALLENGES

    No Of Digits in Rust, explained

    Problem overview

    The function no_of_digits takes a non-negative n, at most 1_000_000_000, and returns how many digits it has. The number 0 has one digit.

    For n = 542 the answer is 3. For n = 100 it is also 3: the zeros are digits too.

    Approach

    Dividing an integer by 10 removes its last digit: 542 / 10 is 54. If you count how many divisions it takes to reach 0, you have counted the digits.

    For 542 the loop runs three times:

    passnum after the passcount after the pass
    1541
    252
    303

    There is one number the loop gets wrong. For 0 the condition num > 0 is false from the start, so the loop would count no digits, yet 0 is written with one. A check at the top handles it.

    Step by step

    1. Return 1 when n is 0. This is the only input the loop can't count.
    2. Copy n into num with let mut. Function parameters are immutable, so the loop needs its own variable to change.
    3. Create count at 0. It counts the digits removed so far.
    4. Loop while num > 0. One pass is one digit.
    5. Add 1 to count with += 1. Rust has no ++ operator.
    6. Replace num with num / 10 to drop the last digit.
    7. Return count as the last expression.

    The solution in Rust

    impl Solution {
        fn no_of_digits(n: i32) -> i32 {
            if n == 0 {
                return 1;
            }
            let mut num = n;
            let mut count = 0;
            while num > 0 {
                count += 1;
                num = num / 10;
            }
            count
        }
    }
    
    • Early return. Inside the if block, return 1; leaves the function at once. The final line, count, is the value for every other path.
    • No ++. Rust spells it count += 1.
    • Integer division. Two i32 operands make / an integer division, so 542 / 10 is 54 and not 54.2.

    Why the loop is enough

    A number with d digits becomes a number with d - 1 digits after one division by 10, and a one-digit number becomes 0. So the number of divisions needed to reach 0 is exactly d, for every positive n. That argument fails for 0 only because 0 is already at the target before the first division, which is the reason for the special case.

    The same idea can be written without a loop, by converting the number to text and taking its length, but the lesson practises the arithmetic version, which works on the number itself.

    Digits that are zeros need no special care. For 100 the loop divides to 10, then 1, then 0, and counts three passes, just as it does for 542.

    Edge cases and common mistakes

    • Zero. Without the early return, no_of_digits(0) would give 0, because the loop body never runs.
    • Trailing zeros. 100 gives 3 and 1_000_000_000, the largest input, gives 10.
    • One digit. 7 gives 1: one pass takes it to 0.
    • Counting after the division. The order of the two statements inside the loop doesn't change the result, since the condition is only checked at the top of each pass.

    Testing

    • n = 542 gives 3.
    • n = 100 gives 3.
    • n = 0 gives 1.
    • n = 7 gives 1.
    • n = 1000000000 gives 10.
    Keep going with Rust1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free