Understanding Functions in Rust

Functions are essential programming constructs since they lay a foundation for code reusability and facilitate modular programming. Functions are self-contained blocks of code that perform specific tasks. They can receive input, in the form of arguments, and return a value.

Rust provides functions for code organization, encapsulation, and code reusability.

4

Defining Functions in Rust

Rust functions are much likefunctions in any other programming language, although there are small differences you’ll need to understand.

You’ll define functions for your Rust programs with thefnkeyword followed by the function name, optional arguments, and an optional returndata type.

Rust programming language logo

Here’s a simple Rust function that doesn’t take in any arguments or return any values.

a_functionis a simple Rust function that prints the variablex.

Jetbrains Rust Functions Preview

Rust Function Signatures

Function signatures are a way of naming functions, and describing their arguments and return types, without including a function body. Function signatures are useful for documenting APIs of Rust libraries.

Here’s an example Rust function signature:

Result from callling Rust functions example

Thenamefunction is a minimal function signature, while thegreetsignature specifies that the function takes a single argument,name, of type string (&str).

Declaring Functions With Arguments and Return Values

Rust functions can take in many arguments, and the limit isn’t explicitly defined. Functions with arguments are generally more flexible since they can take in values from other functions and parts of code.

Here’s the typical model of a Rust function that takes in arguments:

Rust programming language logo

The function argument type can be a Rust built-in type or a custom type from your program.

Here’s an example of a simple function that takes two integers as arguments:

Theadd_numbersfunction takes in two 32-bit integers and prints the sum of the integers.

Rust functions can return multiple values; you’ll have to specify the return type(s) and return the values of the type from the function.

Here’s a function that takes in a string and a 32-bit integer as an argument and returns the arguments as a tuple.

Thestring_and_integerfunction takes in a string and a 32-bit integer, returning them as a tuple. it’s possible to only return a single value from a Rust function.

you could leave out thereturnkeyword when returning the final expression from a function, to make the code more concise.

These two functions have the same behavior since they both take in a string and integer and return those arguments as a tuple.

Calling Rust Functions

you may call one function from another by writing its name followed by values you want to pass to it inside parentheses:

Themainfunction callsadd_numbers, passing it two integers. It assigns the result of the function to a variable,result.

You Can Declare Functions for Rust Structs

you’re able to declare functions within Rust structs. These become methods for the struct that can access and modify it.

Rust isn’t purely object-oriented, but it provides structs to group related data. You can work towards implementing OOP concepts in Rust using structs with methods.

Learn how you can use this custom data type to improve the structure of your Rust programs.

I plugged random USB devices into my phone and was pleasantly surprised by how many actually worked.

So much time invested, and for what?

When your rival has to bail out your assistant.

Now, I actually finish the books I start.

I found my TV was always listening—so I shut it down.

Technology Explained

PC & Mobile