-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path05_Functions.rs
More file actions
28 lines (21 loc) · 764 Bytes
/
05_Functions.rs
File metadata and controls
28 lines (21 loc) · 764 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
fn main() {
greet(); // function with no parameters and no return value
let sum = add(5, 7); // function with parameters and return value
println!("Sum is: {}", sum);
let squared = square(4); // expression as return
println!("Square is: {}", squared);
let nothing = do_nothing(); // function returns unit type ()
println!("Nothing returns: {:?}", nothing);
}
fn greet() {
println!("Hello, Rust!"); // statement
}
fn add(a: i32, b: i32) -> i32 {
return a + b; // return value using 'return' keyword
}
fn square(x: i32) -> i32 {
x * x // return as expression (no semicolon)
}
fn do_nothing() {
// no return value, returns ()
}