Start learning free

LESSON 53 OF 414 · CALCULATIONS - CHALLENGES

Your First Challenge in Rust: Sum of Two Numbers

Practice Read the full explanation

In this lesson we will introduce you to how challenges work and how to make the most out of them. Let's check your first coding challenge:


Sum of Two Numbers

You are given two numbers a and b and you should return their sum.

Examples:

Input: a = 4, b = 9

Output: 13

Explanation:
The sum of a and b is 4 + 9, which is 13.

This is the challenge statement. It explains what the challenge asks us to do and provides some examples to make sure we understand the statement.

Each challenge gives us some input and asks us to write some code that successfully takes that input and uses it to produce some desired output.

In this case, the input is two numbers a and b and the output is their sum.


The class:

If you look at the code editor, you will see that everything is wrapped up inside an impl block for a struct:

struct Solution;

impl Solution {

}

You don't need to know anything about structs or that impl Solution keyword at this point, we will study them in future lessons. Just leave that code untouched.


The function:

Inside this block, we will always be provided with a function. Inside this function is where we should write our solution.

In our case the function is:

fn sum(a: i32, b: i32) -> i32 {
    // Your solution goes here!
}

Parameters and return:

This function's parameters will always match the input described in the challenge's statement. In our case, the parameters are a and b, the two numbers we are given.

The function should return a value which represents the desired output described in the challenge's statement. In our case, it should return a + b.


Assignment:

Follow the Coding Tutorial and let's solve our first coding challenge: Sum of Two Numbers


STEP 1 OF 1
    Ctrl + Enter to check

    RUST · CALCULATIONS - CHALLENGES

    Your First Challenge in Rust, explained

    Problem overview

    Every challenge gives you some input and asks for code that turns it into the desired output. This first one is small on purpose, so you can learn how challenges are laid out. You get two numbers, a and b, and you return their sum. For a = 4 and b = 9 the answer is 13, because 4 + 9 is 13.

    How a challenge is laid out

    The impl block

    The editor shows a frame around your code:

    struct Solution;
    
    impl Solution {
    
    }
    

    You don't need to understand it yet. In short, struct Solution; declares a named type, and impl Solution is where the functions that belong to it are written. Leave both untouched and write your code inside the braces of the function you are given.

    The function signature

    Each challenge provides one function. Your solution goes inside its body. Here it is:

    fn sum(a: i32, b: i32) -> i32 {
        // Your solution goes here!
    }
    

    The signature tells you everything about the contract:

    • The parameters a and b match the input in the statement. Each one has a type after the colon: i32 is a 32-bit signed integer.
    • The arrow -> i32 says the function must give back an i32, which is the output.

    The function does not print its answer. It returns it, and the lesson's tests read the returned value and compare it with the expected one.

    Step by step

    1. Add the two parameters with the + operator. The expression a + b has the value 13 when the parameters are 4 and 9.
    2. Return that value, so the caller receives it.

    The solution in Rust

    impl Solution {
        fn sum(a: i32, b: i32) -> i32 {
            // Write the first step below:
            return a + b;
        }
    }
    

    The keyword return hands the sum to whoever called sum, and the function ends there.

    return is optional at the end

    Rust also returns the last expression of a function when it has no semicolon, so the body could simply be:

    a + b
    

    Both forms do the same thing. Adding a semicolon to this one, a + b;, turns the expression into a statement and the function would return nothing, which the compiler rejects with a mismatched types error because the function promised an i32. The solution above uses return a + b;, with its semicolon, and that is valid because return is a statement that does the handing back itself.

    Complexity

    One addition takes constant time and no extra memory, so the time and space are both O(1).

    Edge cases and common mistakes

    • Negative numbers and zero. sum(-3, 3) is 0 and sum(0, 0) is 0. The same expression handles them with no special case.
    • Overflow. An i32 holds numbers up to 2147483647. In the optimized builds the lessons use, adding past that limit wraps around silently: the sum of 2147483647 and 1 comes out as -2147483648. It is good to know that the type has limits. In a debug build the same addition panics instead.
    • Printing instead of returning. Writing println!("{}", a + b); shows the number but gives the caller nothing back, and the function would not match its -> i32 signature.
    • Editing the frame. Changing the signature or the lines around it breaks the connection between your code and the lesson's tests. Write only in the body.

    Key takeaways

    • A challenge has an input described in the statement, which becomes the parameters, and an output, which is the return value.
    • Your code goes inside the function body in the impl Solution block.
    • Return the result rather than printing it.
    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free