Skip to content

Commit 728556a

Browse files
committed
Feat: add functions section
1 parent 4c5a2ec commit 728556a

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

FUNCTIONS/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

FUNCTIONS/Cargo.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[package]
2+
name = "FUNCTIONS"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[dependencies]

FUNCTIONS/src/main.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Rust code uses snake case as the conventional style for function and variable names
2+
3+
// fn main() {
4+
// print_labeled_measurement(5, 'h');
5+
// }
6+
7+
// fn print_labeled_measurement(value: i32, unit_label: char) {
8+
// println!("The measurement is: {value}{unit_label}");
9+
// }
10+
11+
// ============ Statements & Expressions ============
12+
// Statements are instructions that perform some action and do not return a value
13+
// Expressions evaluate to a resulting value
14+
15+
// Calling a function is an expression.
16+
// Calling a macro is an expression.
17+
// Expressions do not include ending semicolons. If you add a semicolon to the end of an expression, you turn it into a statement, and it will then not return a value.
18+
19+
// fn main() {
20+
// let y = {
21+
// let x = 3;
22+
// x + 1
23+
// };
24+
25+
// println!("The value of y is: {y}");
26+
// }
27+
28+
// ============ Functions with Return Values ============
29+
// We don’t name return values, but we must declare their type after an arrow (->)
30+
// You can return early from a function by using the return keyword and specifying a value, but most functions return the last expression implicitly.
31+
32+
// Example 1
33+
// fn five() -> i32 {
34+
// 5
35+
// }
36+
37+
// fn main() {
38+
// let x = five();
39+
40+
// println!("The value of x is: {x}");
41+
// }
42+
43+
// Example 2
44+
fn main() {
45+
let x = plus_one(5);
46+
47+
println!("The value of x is: {x}");
48+
}
49+
50+
fn plus_one(x: i32) -> i32 {
51+
x + 1
52+
}

0 commit comments

Comments
 (0)