January 2019
Beginner to intermediate
554 pages
13h 31m
English
Functions abstract a bunch of instructions into named entities, which can be invoked later by other code and help manage complexity. We already used a function in our greet.rs program, that is, the main function. Let's look at how we can define another one:
// functions.rsfn add(a: u64, b: u64) -> u64 { a + b}fn main() { let a: u64 = 17; let b = 3; let result = add(a, b); println!("Result {}", result);}
In the preceding code, we created a new function named add. The fn keyword is used to create functions followed by its name, add, its parameters inside parentheses a and b, and the function body inside {} braces. The parameters have their type on the right, after the colon :. Return types in functions are specified using a ->, followed ...
Read now
Unlock full access