Playing with Functions in Rust
Itachi Uchiha
Posted on July 19, 2020
This post published on my blog before
Hi everyone. Before that, I wrote a post called A Trip to Data Types in Rust.
Today I'll try to explain functions in Rust.
Before starting, I'll create a project with cargo;
cargo new functions
cd functions
Introduction
Functions are common in the Rust programming language. We're already creating a function for each program. We're calling it main
. Yes, main
is the most important function in the Rust programming language. This function is the entry point of our programs.
What is A Function
A function is a group of statements. Their main purpose performing tasks you specified. A function can take parameters and can return them. Statements in the functions run in a block called function body or function scope.
Creating a Function
We're using the function
keyword to create functions in JavaScript, PHP, and some other programming languages. Go use the func
keyword to define a function.
We'll use the fn
keyword to define a new function in Rust. This is our very first function;
fn hello() {
println!("Hello everyone!");
}
Yes, we've created our first function. When you call it, it will say Hello everyone!. Rust uses a snake case style for function and variable names. But it allows you about any type of style.
fn hello_world() {
println!("Hello world!");
}
Let's call these functions in the main function.
fn main() {
hello();
hello_world();
}
Functions Parameters
Parameters are inputs. They can be variable or directly passed values. All parameters must have a type. Because anonymous parameters removed in the 2018 edition. Let's create a function with parameters;
fn user(age: u8, name: String) {
println!("I'm {} years old. My name is {}", age, name);
}
fn main() {
user(27, "Ali".to_string());
}
// I'm 27 years old. My name is Ali
Functions can take the types you defined. For example, it can be a struct like a type. But this is another topic. We'll see it later.
Function Bodies
Function bodies are made up of a series of statements optionally ending in an expression. So far, we’ve only covered functions without an ending expression, but you have seen an expression as part of a statement. Because Rust is an expression-based language, this is an important distinction to understand. Other languages don’t have the same distinctions, so let’s look at what statements and expressions are and how their differences affect the bodies of functions.
For example let age: u8 = 10;
is a statement. Because let
keyword is a statement. Functions definitions are also statements in Rust. Statements don't return values. So, if you try to write a statement like below, you'll get an error.
fn main() {
let age = (let my_age = 12);
}
In this case, the compiler expects an expression but it found a statement. This isn't allowed Rust. Let's see an example of expression;
fn main() {
let born_year = 1993;
let year = {
let born_year = 1990;
born_year + 1
};
println!("Born Year {}", year);
}
The year
variable holds an expression. In this expression, let born_year = 1993;
statement won't change. Because year expression is a different scope. I'll try to explain like that;
let born_year = 1993; // IDENTITY NUMBER: 11131211113
and
let year = {
let born_year = 1990; // IDENTITY NUMBER: 0192585111
}
So, these are different. They don't know each other. We didn't use a semi-colon in this part;
born_year + 1
This tells the compiler to return a value.
Return Values
In all programming languages, functions can return values. In some dynamically programming languages don't have to annotate the type of the return value. But you should annotate type of the return value in Rust.
fn sum_two_values(number1: i32, number2: i32) -> i32 {
return number1 + number2;
}
Return a Value without Return Keyword
You can return values without a return
keyword. Because the last line doesn't end a semi-colon.
fn sum_two_values(number1: i32, number2: i32) -> i32 {
number1 + number2
}
fn main() {
println!("Sum Two Values {}", sum_two_values(3, 5));
}
// Sum Two Values 8
That's all for now. In the future, we'll see functions deeply. Thanks for reading!
Resources
Posted on July 19, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.