In Rust, functions can return values to the caller, enabling efficient computations and the smooth delivery of results. This capability is fundamental to writing robust Rust programs.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.

->) in the function signature. The returned value can be the last expression in the function body or explicitly returned using the return keyword.
Below is the standard syntax for a function with a return value:
Example: Adding Two Integers
The following example demonstrates a function calledadd that accepts two integers and returns their sum. The main function is the entry point of the program, calling add with the arguments 5 and 3:
add function, the expression a + b is the last statement and is implicitly returned. When this program is executed, it prints: “The sum is: 8” to the console.
Example: Subtracting Two Integers with an Explicit Return
Rust also allows using thereturn keyword for explicitly returning a value. Consider the subtract function in the example below, which explicitly returns the result of subtracting b from a:
Rust functions can return values of any type, including complex types like strings, tuples, and even other functions.
Example: Returning a Greeting String
In the following example, a function returns a greeting string by formatting a message with the provided name:Summary of Key Concepts
- Use the arrow syntax (
->) to specify a function’s return type. - The last expression in a function is returned implicitly.
- Use the
returnkeyword to explicitly return a value. - Functions in Rust can output various data types including integers, strings, and tuples.
