11use std:: collections:: HashMap ; // VER: define_map
2+ use std:: collections:: HashSet ; // VER: define_set
3+
24
35//fn main() { // VER: hello_world
46fn hello_world ( ) {
@@ -58,6 +60,49 @@ fn if_statement() {
5860 } // VER: if_statement
5961}
6062
63+ fn for_loop ( ) {
64+ let data = [ 5 , 6 , 7 ] ; // VER: for_loop
65+ for i in 0 ..data. len ( ) { // VER: for_loop
66+ println ! ( "{}" , data[ i] ) ; // VER: for_loop
67+ } // VER: for_loop
68+ }
69+
70+ fn while_loop ( ) {
71+ let mut count = 0 ; // VER: while_loop
72+ while count < 10 { // VER: while_loop
73+ println ! ( "Count is {}" , count) ; // VER: while_loop
74+ count = count + 2 ; // VER: while_loop
75+ } // VER: while_loop
76+ }
77+
78+ fn for_each_loop ( ) {
79+ let ff = [ "a" , "b" , "c" ] ; // VER: for_each_loop
80+ for f in ff { // VER: for_each_loop
81+ println ! ( "{}" , f) ; // VER: for_each_loop
82+ } // VER: for_each_loop
83+ }
84+
85+
86+ fn sayHello ( ) { // VER: function
87+ println ! ( "Hello" ) ; // VER: function
88+ println ! ( "Goodbye" ) ; // VER: function
89+ } // VER: function
90+ fn function ( ) {
91+ sayHello ( ) ; // VER: function
92+ }
93+
94+ fn biggest ( a : i32 , b : i32 ) -> i32 { // VER: function_with_return_value
95+ if a > b { // VER: function_with_return_value
96+ return a; // VER: function_with_return_value
97+ } // VER: function_with_return_value
98+ else { // VER: function_with_return_value
99+ return b; // VER: function_with_return_value
100+ } // VER: function_with_return_value
101+ } // VER: function_with_return_value
102+ fn function_with_return_value ( ) -> ( ) {
103+ println ! ( "{}" , biggest( 1 , 2 ) ) ; // VER: function_with_return_value
104+ }
105+
61106
62107fn define_fixed_array ( ) {
63108 let mut aa: [ & str ; 3 ] = [ "" ; 3 ] ; // VER: define_fixed_array
@@ -131,6 +176,29 @@ fn split_strings() {
131176 println ! ( "{}" , csv_line_test2) ; // VER: split_strings
132177}
133178
179+ fn dict_comprehension ( ) {
180+ let data3 = HashMap :: from ( [ // VER: dict_comprehension
181+ ( "a" , 1 ) , // VER: dict_comprehension
182+ ( "b" , 2 ) , // VER: dict_comprehension
183+ ( "c" , 3 ) , // VER: dict_comprehension
184+ ] ) ; // VER: dict_comprehension
185+ let data4 = data3. iter ( ) // VER: dict_comprehension
186+ . filter ( |( & k, & v) | v>=2 ) // VER: dict_comprehension
187+ . map ( |( & k, & v) | ( v+10 , k) ) // VER: dict_comprehension
188+ . collect :: < HashMap < _ , _ > > ( ) ; // VER: dict_comprehension
189+ println ! ( "{:?}" , data4) ; // VER: dict_comprehension
190+ }
191+
192+ fn define_set ( ) {
193+ let mut aa: HashSet < _ > = vec ! [ 1 , 2 , 3 ] . into_iter ( ) . collect ( ) ; // VER: define_set
194+ let bb: HashSet < _ > = vec ! [ 2 , 3 , 4 ] . into_iter ( ) . collect ( ) ; // VER: define_set
195+ let cc: HashSet < _ > = vec ! [ 1 , 2 ] . into_iter ( ) . collect ( ) ; // VER: define_set
196+ println ! ( "{:?}" , aa. union ( & bb) . collect:: <Vec <_>>( ) ) ; // VER: define_set
197+ println ! ( "{:?}" , aa. intersection( & bb) . collect:: <Vec <_>>( ) ) ; // VER: define_set
198+ println ! ( "{:?}" , aa. difference( & bb) . collect:: <Vec <_>>( ) ) ; // VER: define_set
199+ println ! ( "{:?}" , cc. is_subset( & aa) ) ; // VER: define_set
200+ aa. insert ( 5 ) ; // VER: define_set
201+ }
134202
135203
136204fn main ( ) {
@@ -139,9 +207,16 @@ fn main() {
139207 define_constants ( ) ;
140208 arithmetic ( ) ;
141209 if_statement ( ) ;
210+ for_loop ( ) ;
211+ while_loop ( ) ;
212+ for_each_loop ( ) ;
213+ function ( ) ;
214+ function_with_return_value ( ) ;
142215 define_fixed_array ( ) ;
143216 define_list ( ) ;
144217 define_map ( ) ;
145218 string_concatenation ( ) ;
146219 split_strings ( ) ;
220+ dict_comprehension ( ) ;
221+ define_set ( ) ;
147222}
0 commit comments