Start learning free

LESSON 53 OF 348 · CALCULATIONS - CHALLENGES

Your First Challenge in Rust

Practice

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 succesfully 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 a class:

struct Solution;

impl Solution {

}

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


The function:

Inside this class, 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 method's parameters will always match the input described in the challange's statement. In our case, the parameters are a and b, the two numbers we are given.

The method should return a value which represents the desired output described in the challange'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

    Your First Challenge in Rust

    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free