Getting Started with Rust Using Rustlings — Part 2: Functions

Jesse Verbruggen
3 min readFeb 13, 2023

--

Part 2: Functions

In my last part, I went over Variables in Rust. This is Part two of my miniseries on Rust. If you want to read the last part, you can get to it by following the link to my article below.

In this series, I am going over my process of learning the syntax of Rust with Rustlings. In my first Part, I explain how to install Rust and Rustlings. So if you haven’t yet done that, please navigate there to follow along.

functions1.rs

fn main() {
call_me();
}

In this exercise, we will build our first function in Rust and have it print “Hello World!”. We will use the signature of the call_me() and create a function with its name.

fn main() {
call_me();
}

fn call_me() {
println!("Hello, World!");
}

And voila, this code will compile and output “Hello, World!” to stdout using the built-in function println.

functions2.rs

fn main() {
call_me(3);
}

fn call_me(num:) {
for i in 0..num {
println!("Ring! Call number {}", i + 1);
}
}

Now in this exercise, the function call_me takes an argument called num but it does not have a type that the compiler needs to create a function signature. This is required in strongly typed languages, so the function knows what type of variables it will receive. In this case, we can add the i32 type to num.

fn main() {
call_me(3);
}

fn call_me(num : i32) {
for i in 0..num {
println!("Ring! Call number {}", i + 1);
}
}

functions3.rs

fn main() {
call_me();
}

fn call_me(num: u32) {
for i in 0..num {
println!("Ring! Call number {}", i + 1);
}
}

In this scenario, the function we try to call does not have the required parameter. In this case, we can add a number to the function call, and the code will compile and run successfully.

fn main() {
call_me(3);
}

fn call_me(num: u32) {
for i in 0..num {
println!("Ring! Call number {}", i + 1);
}
}

functions4.rs

fn main() {
let original_price = 51;
println!("Your sale price is {}", sale_price(original_price));
}

fn sale_price(price: i32) -> {
if is_even(price) {
price - 10
} else {
price - 3
}
}

fn is_even(num: i32) -> bool {
num % 2 == 0
}

For the next exercise, we are looking at function return values. In this case, we want to ensure the function sale_price returns an integer we can print to the console. We can do this by adding a return type to the function behind the arrow operator ->.

fn main() {
let original_price = 51;
println!("Your sale price is {}", sale_price(original_price));
}

fn sale_price(price: i32) -> i32 {
if is_even(price) {
price - 10
} else {
price - 3
}
}

fn is_even(num: i32) -> bool {
num % 2 == 0
}

functions5.rs

fn main() {
let answer = square(3);
println!("The square of 3 is {}", answer);
}

fn square(num: i32) -> i32 {
num * num;
}

So what’s wrong with this final problem? Well, it’s pretty straightforward. All functions with return types need to return a value. In this case, we’re missing the return keyword, so this code cannot compile. Let’s add it and finish the final problem.

fn main() {
let answer = square(3);
println!("The square of 3 is {}", answer);
}

fn square(num: i32) -> i32 {
return num * num;
}

Alright, that’s all the exercises I will go over right now. I hope you enjoyed following along with me. As I complete these exercises, join me to start your journey into Rust. The next part about If and Primitive Types in Rust is out now. Follow the link below to get to it.

If I helped you learn more about Rust or you found this interesting, please clap 👏 and share this article to help more people find these articles.

Let’s help each other learn and grow! Peace out✌️

--

--