Functions

In this article we will cover basic functions in Rust

Simple functions

In Rust we can create multiple functions apart from the main one. Here is a little example of how to do it:

1
2
3
4
5
6
7
8
9
fn main() {
	printn!("Hey, how are you doing!")

	hello_friend(); // We are calling a function called hello_there
}

fn hello_there() {  // We've created a new function called hello_there
	println!("Hello my friend!");
}

Rust code uses snake case as the conventional style. This means all lowercase and underscore separated words.

Parameterized functions

Functions can have parameters. Parameters or arguments are variables that are only in the scope of the function code. This means it cannot be accessed anywhere outside the function block. The arguments values are provided by the script.

1
2
3
4
5
6
7
8
fn main() {
	println!("Let's do some functions");
	addition(5);
}

fn addition (x: u32) {
	println!("The values is: {}", x);
}

When we use arguments, we must specify the argument data type.

Theme Stack designed by Jimmy