Rust 101: Functions

Mukundh Bhushan
4 min readMay 24, 2020

--

In the previous article, we discussed about conditional statements in rust. Now let’s dive into Functions.

TL;DR

  • Functions
  • Function which returns no value
  • Functions which returns a value
  • Main
  • Importing a function which is in a different file

First, let’s start off by creating a new project called “Functions” using the following command

cargo new functions --bin

Functions

Functions are used when you have to execute a piece of code repeatedly without the need to write the code again and again.

Here is the syntax to create a function in Rust

//public function
pub fn <function name>(<arg>:<datatype>) -> <return type>{
//code block}//private function
fn <function name>(<arg>:<datatype>) -> <return type>{
//code block}

pub — is the public access specifier. If a function is private then the access specifier section is left empty.

Function which returns no value

These functions accept arguments but return no value.

Here is the syntax

pub fn <function name>(<arg>:<datatype>){}

Here is a sample code

The above code finds the sum of the given two numbers.

We can provide an array as a function argument.

Here is the code which finds the sum of a given array

Functions which return a value

Now, let's see how we can write the above functions but this time the function returns a value instead of printing the answer directly.

Here is the code

Returning a Vector instead of a single value

In the following sample we add 10 to every element in a given vector

Note: Considers using vectors over arrays when dealing with functions. Arrays require you to use pointers and references instead of the values contained in the array, Vectors use values instead.

Main

Similar to all the others programming languages “main” is the entry point of a program in Rust. Main takes no arguments and can be either public or a private function. For a rust program to run the Main function is always needed. It's a good practise in rust to have the main function in the “main.rs” file.

Here is the syntax

pub fn main() {   //code block}

Importing a function which is in a different file

This is my file structure for my rust project:

mediumrust/Collections/
├── src
│ ├── main.rs
│ └── add.rs
├── target
│ ├── debug
│ ├── rls

├── Cargo.lock
├── Cargo.toml

Under the “src” folder I have 2 files main.rs and add.rs. Let’s implement two functions one which adds two given numbers and the other which adds all sum of all numbers in a given. vector. But this time in the “add.rs” file instead.

The “mod” keyword is used to import the function in the file. The function which can be imported must have the “pub” keyword which makes the function public, Private functions cannot be imported.

Unlike. many other programming languages which use the “.” operator, Rust uses the “::” operator to call the functions in the imported file or library.

Here in the syntax

//to import
mod <file name>;
//To use the functions
let <var name> = <file name>::<function name>;

We will be learning more about mods in the upcoming articles.

Code in add.rs

Notice both the functions have the public access specifier.

Code in main.rs file

Note: if when we are importing files using file names we don't have to add the “.rs” after the file name

In the next article, we will discuss about Modules(mod).

link to the next article

//yet to be written hold on tight

Link to the previous article

--

--