File tree 8 files changed +117
-0
lines changed
8 files changed +117
-0
lines changed Original file line number Diff line number Diff line change @@ -58,3 +58,4 @@ in a controlled repository now.
58
58
* ` iterator-example.rs ` : example of Rust iterator
59
59
* ` lifetimes-example.rs ` : example involving Rust lifetimes
60
60
* ` strref.rs ` : example involving Rust string clones and copies
61
+ * ` closures/ ` : closure examples
Original file line number Diff line number Diff line change
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.
Original file line number Diff line number Diff line change
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"
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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"
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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"
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments