-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from tainguyenbp/feat/learn-programming-rust-la…
…ng03 test pointer and closures
- Loading branch information
Showing
14 changed files
with
56 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
fn main() { | ||
let x = 4; | ||
println!("{}", get_square_value(x)); | ||
} | ||
|
||
fn get_square_value(i: i32) -> i32 { | ||
i * i | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
fn main() { | ||
let x = 65536; | ||
let square = |i: i32| -> i32 { // Input parameters are passed inside | | and expression body is wrapped within { } | ||
i * i | ||
}; | ||
println!("{}", square(x)); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
fn main() { | ||
let x = 40; | ||
let square = |i| i * i; // { } are optional for single-lined closures | ||
println!("{}", square(x)); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
fn main() { | ||
let x = 300; | ||
let x_square = |i: i32| -> i32 { i * i }(x); // { } are mandatory while creating and calling same time. | ||
println!("{}", x_square); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
fn main() { | ||
let x = 68; | ||
let x_square = |i| -> i32 { i * i }(x); // ⭐️ The return type is mandatory. | ||
println!("{}", x_square); | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
|
||
fn main() { | ||
// 01. Without type declarations. | ||
let p1 = plus_one; | ||
let x = p1(9); // 6 | ||
|
||
println!("Result: {}", x); | ||
|
||
} | ||
|
||
|
||
fn plus_one(a: i32) -> i32 { | ||
a + 1 | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
fn main() { | ||
// 02. With type declarations. | ||
let p1: fn(i32) -> i32 = plus_one; | ||
let x = p1(7); | ||
|
||
println!("Result: {}", x); | ||
} | ||
|
||
fn plus_one(a: i32) -> i32 { | ||
a + 1 | ||
} |