File tree Expand file tree Collapse file tree 13 files changed +302
-0
lines changed
Expand file tree Collapse file tree 13 files changed +302
-0
lines changed Original file line number Diff line number Diff line change @@ -85,3 +85,29 @@ These examples require setting an environment variable. How to do that in your e
8585- ` MARKDOWN=README.md cargo run -p day-14-thiserror `
8686- ` MARKDOWN=README.md cargo run -p day-14-error-chain `
8787- ` MARKDOWN=README.md cargo run -p day-14-snafu `
88+
89+ ### Day 16
90+
91+ #### Closures
92+
93+ - ` cargo run -p day-15-closures `
94+
95+ ### Day 16
96+
97+ #### Type differences
98+
99+ - ` cargo run -p day-16-type-differences --bin impl-owned `
100+ - ` cargo run -p day-16-type-differences --bin impl-borrowed `
101+
102+ #### ` 'static `
103+
104+ - ` cargo run -p day-16-static ` - intentionally does not compile
105+ - ` cargo run -p day-16-static-bounds ` - intentionally does not compile
106+
107+ #### Lifetime elision
108+
109+ - ` cargo run -p day-16-elision `
110+
111+ #### Unsafe pointers
112+
113+ - ` cargo run -p day-16-unsafe-pointers `
Original file line number Diff line number Diff line change 1+ [package ]
2+ name = " day-16-elision"
3+ version = " 0.0.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 ]
Original file line number Diff line number Diff line change 1+ fn omits_annotations ( list : & [ String ] ) -> Option < & String > {
2+ list. get ( 0 )
3+ }
4+
5+ fn has_annotations < ' a > ( list : & ' a [ String ] ) -> Option < & ' a String > {
6+ list. get ( 1 )
7+ }
8+
9+ fn main ( ) {
10+ let authors = vec ! [ "Samuel Clemens" . to_owned( ) , "Jane Austen" . to_owned( ) ] ;
11+ let value = omits_annotations ( & authors) . unwrap ( ) ;
12+ println ! ( "The first author is '{}'" , value) ;
13+ let value = has_annotations ( & authors) . unwrap ( ) ;
14+ println ! ( "The second author is '{}'" , value) ;
15+ }
Original file line number Diff line number Diff line change 1+ [package ]
2+ name = " day-16-static-bounds"
3+ version = " 0.0.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 ]
Original file line number Diff line number Diff line change 1+ use std:: fmt:: Display ;
2+
3+ fn main ( ) {
4+ let mut string = "First" . to_owned ( ) ;
5+
6+ string. push_str ( string. to_uppercase ( ) . as_str ( ) ) ;
7+ print_a ( & string) ;
8+ print_b ( & string) ;
9+ print_c ( & string) ; // Compilation error
10+ print_d ( & string) ; // Compilation error
11+ print_e ( & string) ;
12+ print_f ( & string) ;
13+ print_g ( & string) ; // Compilation error
14+ }
15+
16+ fn print_a < T : Display + ' static > ( t : & T ) {
17+ println ! ( "{}" , t) ;
18+ }
19+
20+ fn print_b < T > ( t : & T )
21+ where
22+ T : Display + ' static ,
23+ {
24+ println ! ( "{}" , t) ;
25+ }
26+
27+ fn print_c ( t : & ' static dyn Display ) {
28+ println ! ( "{}" , t)
29+ }
30+
31+ fn print_d ( t : & ' static impl Display ) {
32+ println ! ( "{}" , t)
33+ }
34+
35+ fn print_e ( t : & ( dyn Display + ' static ) ) {
36+ println ! ( "{}" , t)
37+ }
38+
39+ fn print_f ( t : & ( impl Display + ' static ) ) {
40+ println ! ( "{}" , t)
41+ }
42+
43+ fn print_g ( t : & ' static String ) {
44+ println ! ( "{}" , t) ;
45+ }
Original file line number Diff line number Diff line change 1+ [package ]
2+ name = " day-16-static"
3+ version = " 0.0.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 ]
Original file line number Diff line number Diff line change 1+ use std:: fmt:: Display ;
2+
3+ fn main ( ) {
4+ let r1;
5+ let r2;
6+ {
7+ static STATIC_EXAMPLE : i32 = 42 ;
8+ r1 = & STATIC_EXAMPLE ;
9+ let x = "&'static str" ;
10+ r2 = x;
11+ }
12+ println ! ( "&'static i32: {}" , r1) ;
13+ println ! ( "&'static str: {}" , r2) ;
14+
15+ let r3;
16+
17+ {
18+ let string = "String" . to_owned ( ) ;
19+
20+ static_bound ( & string) ; // This is *not* an error
21+ r3 = & string; // This *is*
22+ }
23+ println ! ( "{}" , r3) ;
24+ }
25+
26+ fn static_bound < T : Display + ' static > ( t : & T ) {
27+ println ! ( "{}" , t) ;
28+ }
Original file line number Diff line number Diff line change 1+ [package ]
2+ name = " day-16-type-differences"
3+ version = " 0.0.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 = " impl-owned"
12+ test = false
13+ bench = false
14+ path = " src/impl-owned.rs"
15+
16+ [[bin ]]
17+ name = " impl-borrowed"
18+ test = false
19+ bench = false
20+ path = " src/impl-borrowed.rs"
Original file line number Diff line number Diff line change 1+ use std:: fmt:: Display ;
2+
3+ fn main ( ) {
4+ let my_struct = MyStruct { } ;
5+ // my_struct.print(); // Removed because the impl no longer exists
6+ println ! ( "<intentionally removed this Foo>" ) ;
7+
8+ let struct_ref = & my_struct;
9+ struct_ref. print ( ) ;
10+
11+ let mut mut_struct = MyStruct { } ;
12+ // mut_struct.print(); // Removed because the impl no longer exists
13+ println ! ( "<intentionally removed this Foo>" ) ;
14+
15+ let ref_mut_struct = & mut_struct;
16+ ref_mut_struct. print ( ) ;
17+
18+ let mut_struct_ref = & mut mut_struct;
19+ mut_struct_ref. print ( ) ;
20+ }
21+
22+ trait Printer {
23+ fn print ( & self ) ;
24+ }
25+
26+ struct MyStruct { }
27+
28+ // Watch how the output differs when this impl is removed.
29+ //
30+ // impl Printer for MyStruct {
31+ // fn print(&self) {
32+ // println!("Foo")
33+ // }
34+ // }
35+
36+ impl Printer for & MyStruct {
37+ fn print ( & self ) {
38+ println ! ( "&Foo" )
39+ }
40+ }
41+
42+ impl Printer for & mut MyStruct {
43+ fn print ( & self ) {
44+ println ! ( "&mut Foo" )
45+ }
46+ }
You can’t perform that action at this time.
0 commit comments