Skip to content

Commit 4fbdf17

Browse files
committed
added closure examples
1 parent 0439f8f commit 4fbdf17

File tree

8 files changed

+117
-0
lines changed

8 files changed

+117
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,4 @@ in a controlled repository now.
5858
* `iterator-example.rs`: example of Rust iterator
5959
* `lifetimes-example.rs`: example involving Rust lifetimes
6060
* `strref.rs`: example involving Rust string clones and copies
61+
* `closures/`: closure examples

closures/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This contains various closure things
2+
3+
* `notaclosure`: manual implementation of closure using struct
4+
* `closure`: standard rust closure stuff
5+
* `genclosure`: closures and generic types.

closures/closure/Cargo.toml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "closure"
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]
9+
10+
[[bin]]
11+
name = "closure"
12+
path = "closure.rs"

closures/closure/closure.rs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
fn triple(x: u32) -> u32 {
2+
x + x + x
3+
}
4+
5+
fn foo() {
6+
let mut y = 2;
7+
let mut f: Box<dyn FnOnce(u32) -> u32> = Box::new(|x| { y += 1; x * y });
8+
println!("{}", f(4));
9+
f = Box::new(|x| x + x);
10+
println!("{}", f(4));
11+
f = Box::new(triple);
12+
println!("{}", f(4));
13+
}
14+
15+
fn main() {
16+
foo();
17+
18+
/*
19+
let mut y = 2;
20+
let mut powerer = move |x: u64| { y += 1; u64::pow(x, y) };
21+
println!("{}", powerer(3));
22+
powerer = move |x: u64| { y += 1; u64::pow(x, y) };
23+
println!("{}", powerer(3));
24+
println!("{}", y);
25+
*/
26+
}

closures/genclosure/Cargo.toml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "genclosure"
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]
9+
10+
[[bin]]
11+
name = "genclosure"
12+
path = "genclosure.rs"

closures/genclosure/genclosure.rs

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
fn repeat<F: FnMut(u32)->u32>(mut f: F, x: u32) -> u32 {
2+
let z = f(x);
3+
f(z)
4+
}
5+
6+
struct Mutation(String);
7+
8+
impl Mutation {
9+
fn new<F: FnOnce(String)->String>(mutate: F, s: String) -> Self {
10+
Mutation(mutate(s))
11+
}
12+
}
13+
14+
fn main() {
15+
let mut y = 2;
16+
println!("{}", repeat(|x| {y += 1; x * y}, 3));
17+
18+
let pal = |s: String|->String {s.chars().rev().collect()};
19+
let mutation = Mutation::new(
20+
pal,
21+
"hello".to_string(),
22+
);
23+
println!("{}", mutation.0);
24+
let mutation = Mutation::new(
25+
pal,
26+
"goodbye".to_string(),
27+
);
28+
println!("{}", mutation.0);
29+
}

closures/notaclosure/Cargo.toml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "notaclosure"
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]
9+
10+
[[bin]]
11+
name = "notaclosure"
12+
path = "notaclosure.rs"

closures/notaclosure/notaclosure.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
struct PowerClosure {
2+
y: u32,
3+
}
4+
5+
impl PowerClosure {
6+
fn new(y: u32) -> Self {
7+
Self { y }
8+
}
9+
10+
fn eval(self, x: u64) -> u64 {
11+
u64::pow(x, self.y)
12+
}
13+
}
14+
15+
fn main() {
16+
let y = 2;
17+
let power = PowerClosure::new(y);
18+
println!("{}", power.eval(3));
19+
println!("{}", power.eval(3));
20+
}

0 commit comments

Comments
 (0)