Skip to content

Commit

Permalink
Merge pull request #4 from tainguyenbp/feat/learn-programming-rust-la…
Browse files Browse the repository at this point in the history
…ng03

test pointer and closures
  • Loading branch information
tainguyenbp authored May 29, 2024
2 parents 35d5a9b + 70649bd commit 1381425
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 0 deletions.
Binary file added rust-basic/functions/closures/closures01
Binary file not shown.
8 changes: 8 additions & 0 deletions rust-basic/functions/closures/closures01.rs
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 added rust-basic/functions/closures/closures02
Binary file not shown.
7 changes: 7 additions & 0 deletions rust-basic/functions/closures/closures02.rs
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 added rust-basic/functions/closures/closures03
Binary file not shown.
5 changes: 5 additions & 0 deletions rust-basic/functions/closures/closures03.rs
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 added rust-basic/functions/closures/closures04
Binary file not shown.
5 changes: 5 additions & 0 deletions rust-basic/functions/closures/closures04.rs
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 added rust-basic/functions/closures/closures05
Binary file not shown.
5 changes: 5 additions & 0 deletions rust-basic/functions/closures/closures05.rs
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 added rust-basic/functions/pointers/pointers-data-type
Binary file not shown.
15 changes: 15 additions & 0 deletions rust-basic/functions/pointers/pointers-data-type.rs
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.
11 changes: 11 additions & 0 deletions rust-basic/functions/pointers/pointers-data-type02.rs
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
}

0 comments on commit 1381425

Please sign in to comment.