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
aandbmatch the input in the statement. Each one has a type after the colon:i32is a 32-bit signed integer. - The arrow
-> i32says the function must give back ani32, 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
- Add the two parameters with the
+operator. The expressiona + bhas the value 13 when the parameters are 4 and 9. - 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 andsum(0, 0)is 0. The same expression handles them with no special case. - Overflow. An
i32holds 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-> i32signature. - 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 Solutionblock. - Return the result rather than printing it.
