Functions

The starting point of every Rust program is itself a function fn called the main() function, which can be further subdivided into separate functions for reuse of code or better code organization. Rust doesn't care in which order these functions are defined, but it is nice to put the function main() at the start of the code to get a better overview. Rust has incorporated many features of traditional functional languages; we will see examples of that in Chapter 5, Higher Order Functions and Error-Handling.

Let's start with a basic function example:

// from Chapter 3/code/functions.rs fn main() { let hero1 = "Pac Man"; let hero2 = "Riddick"; greet(hero2); greet_both(hero1, hero2); } fn greet(name: &str) { println!("Hi mighty {}, what ...

Get Rust Essentials - Second Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.