Post

Functions

Functions

Use the fn keyword to declare new functions followed by function name which has to be of snake_case conventional standard and a set of parentheses.

The below function represents the main entry point of a rust program.

1
2
3
fn main(){

}

Create a new function call (add_numbers) and call it from the main function.

1
2
3
4
5
6
7
8
9
10
fn main(){
  println!("hello world");

    add_numbers();

}
fn add_numbers(){
   println!(" add more numbers");
}

You can return early from a function by using the return keyword and specifying the value.

Most functions return the last value implicitly.

1
2
3
4
5
6
7
8
9
10
fn five() ->i32{
5
}

fn main(){
let x = five();

println!("The value of x is: {x}");

}

->i32 this is the functions return type.

This post is licensed under CC BY 4.0 by the author.