Search⌘ K
AI Features

Function With Multiple Return Values

Understand how to return multiple values from a Rust function by using tuples. This lesson demonstrates a function that calculates both area and perimeter of a rectangle and returns them together, helping you manage multiple outputs effectively.

Returning Multiple Values

In system programming languages like C++ and C, it is only possible to return a single value or a pointer to an array from a function. However, Rust allows you to return multiple values using a tuple.

Syntax

The function definition for returning multiple values:

The way to return tuple from a function is to just write the tuple:

...