1- fn main ( ) {
1+ fn main ( ) {
2+ // PART 1: &str vs String
23
3- // PART 1: &str vs String
4+ // &str --> fized text baked into your program
5+ let fixed: & str = "hello world" ;
6+ println ! ( "&str: {}" , fixed) ;
47
5- // &str --> fized text baked into your program
6- let fixed : & str = "hello world" ;
7- println ! ( "&str : {}" , fixed ) ;
8+ // String --> owned, growable txt
9+ let owned : String = String :: from ( "hello world" ) ;
10+ println ! ( "String : {}" , owned ) ;
811
9- // String --> owned, growable txt
10- let owned: String = String :: from ( "hello world" ) ;
11- println ! ( "String: {}" , owned) ;
12+ // convert &str --> String
13+ let s1 = "help" . to_string ( ) ;
14+ let s2 = String :: from ( "me please" ) ;
15+ println ! ( "both equal: {}" , s1 == s2) ; // true
1216
13- // convert &str --> String
14- let s1 = "help" . to_string ( ) ;
15- let s2 = String :: from ( "me please" ) ;
16- println ! ( "both equal : {}" , s1 == s2 ) ; // true
17+ // convert String --> &str
18+ let owned = String :: from ( "hello" ) ;
19+ let borrowed : & str = & owned ; // borrow it as &str
20+ println ! ( "borrowed : {}" , borrowed ) ;
1721
18- // convert String --> &str
19- let owned = String :: from ( "hello" ) ;
20- let borrowed : & str = & owned; // borrow it as &str
21- println ! ( "borrowed: {}" , borrowed) ;
22+ // PART 2: Building Strings
2223
23- // PART 2: Building Strings
24+ // push_str --> appedn a &str
25+ let mut s = String :: from ( "hello" ) ;
26+ s. push_str ( " Universe" ) ;
27+ println ! ( "{} " , s) ;
2428
25- // push_str --> appedn a &str
26- let mut s =String :: from ( "hello" ) ;
27- s. push_str ( " Universe" ) ;
28- println ! ( "{} " , s) ;
29+ // push --> append a single character
30+ s. push ( '!' ) ;
31+ println ! ( "{}" , s) ;
2932
30- // push --> append a single character
31- s. push ( '!' ) ;
32- println ! ( "{}" , s) ;
33+ // + operator --> concatenates
34+ // NOTE: Consumes first string!!
35+ let s1 = String :: from ( "Is this a real life " ) ;
36+ let s2 = String :: from ( "or just fantasy " ) ;
3337
34- // + operator --> concatenates
35- // NOTE: Consumes first string!!
36- let s1 = String :: from ( "Is this a real life " ) ;
37- let s2 = String :: from ( "or just fantasy " ) ;
38-
39- let s3 = s1 + & s2; // s1 is MOVED here --> can't use s1 after this
38+ let s3 = s1 + & s2; // s1 is MOVED here --> can't use s1 after this
4039
41- println ! ( "{}" , s3) ;
40+ println ! ( "{}" , s3) ;
4241
43- // format! macro --> joins w/o consuming (most flexible)
44- let first = String :: from ( "Caught in a landscape, " ) ;
45- let sec = String :: from ( "no escape from reality" ) ;
46- let combined = format ! ( "{} {}!" , first, sec) ;
42+ // format! macro --> joins w/o consuming (most flexible)
43+ let first = String :: from ( "Caught in a landscape, " ) ;
44+ let sec = String :: from ( "no escape from reality" ) ;
45+ let combined = format ! ( "{} {}!" , first, sec) ;
4746
48- println ! ( "{}" , combined) ; //NOTE: first & sec are still valid -- format! borrows them
47+ println ! ( "{}" , combined) ; //NOTE: first & sec are still valid -- format! borrows them
4948
50- // repeat
51- let repeated = "ha" . repeat ( 3 ) ;
52- println ! ( "{}" , repeated) ;
49+ // repeat
50+ let repeated = "ha" . repeat ( 3 ) ;
51+ println ! ( "{}" , repeated) ;
5352
54- // PART 3: Trimming
55- let s = " Namaste India " ;
53+ // PART 3: Trimming
54+ let s = " Namaste India " ;
5655
57- println ! ( "'{}'" , s. trim( ) ) ; // 'Namaste India'
58- println ! ( "'{}'" , s. trim_start( ) ) ;
59- println ! ( "'{}'" , s. trim_end( ) ) ;
56+ println ! ( "'{}'" , s. trim( ) ) ; // 'Namaste India'
57+ println ! ( "'{}'" , s. trim_start( ) ) ;
58+ println ! ( "'{}'" , s. trim_end( ) ) ;
6059
61- // trim specific characters
60+ // trim specific characters
6261 let s2 = "###hello###" ;
63- println ! ( "{}" , s2. trim_matches( '#' ) ) ;
62+ println ! ( "{}" , s2. trim_matches( '#' ) ) ;
6463
6564 // PART 4: Searching & Checking
6665
@@ -73,91 +72,89 @@ fn main(){
7372
7473 // find --> returns the index of first match, or None
7574 match s. find ( "Rust" ) {
76- Some ( i) => println ! ( "'Rust' found at index {}" , i) ,
77- None => println ! ( "not found" ) ,
75+ Some ( i) => println ! ( "'Rust' found at index {}" , i) ,
76+ None => println ! ( "not found" ) ,
7877 }
7978
8079 // PART 5 : Replacing
8180
8281 let s = String :: from ( "I love Java and Java was my first programming language" ) ;
83-
82+
8483 // replace --> replaces ALL occurences
85- let replaced = s. replace ( "Java" , "Rust" ) ;
86- println ! ( "{}" , replaced) ;
84+ let replaced = s. replace ( "Java" , "Rust" ) ;
85+ println ! ( "{}" , replaced) ;
8786
88- // replacen --> replaces only N occurrences
89- let replaced_once = s. replacen ( "Java" , "Rust" , 1 ) ;
90- println ! ( "{}" , replaced_once) ;
87+ // replacen --> replaces only N occurrences
88+ let replaced_once = s. replacen ( "Java" , "Rust" , 1 ) ;
89+ println ! ( "{}" , replaced_once) ;
9190
92- // PART 6 : Splitting --> most important for CLI tools
91+ // PART 6 : Splitting --> most important for CLI tools
9392
94- let sentence = "hello world foo bar" ;
93+ let sentence = "hello world foo bar" ;
9594
96- let words: Vec < & str > = sentence. split ( ' ' ) . collect ( ) ;
97- println ! ( "split: {:?}" , words) ;
95+ let words: Vec < & str > = sentence. split ( ' ' ) . collect ( ) ;
96+ println ! ( "split: {:?}" , words) ;
9897
99- // splitn --> split into AT MOST n parts
100- // Very useful for parsing commands like "SET key value"
101- let cmd = "SET my key some value with spaces" ;
102- let parts: Vec < & str > = cmd. splitn ( 3 , ' ' ) . collect ( ) ;
103- println ! ( "splitn(3): {:?}" , parts) ;
104- // ["SET", "my", "key some value with spaces"]
105- // NOTICE: everything after the 2nd space stays together
98+ // splitn --> split into AT MOST n parts
99+ // Very useful for parsing commands like "SET key value"
100+ let cmd = "SET my key some value with spaces" ;
101+ let parts: Vec < & str > = cmd. splitn ( 3 , ' ' ) . collect ( ) ;
102+ println ! ( "splitn(3): {:?}" , parts) ;
103+ // ["SET", "my", "key some value with spaces"]
104+ // NOTICE: everything after the 2nd space stays together
106105
107- // split and check first word
108- let input = "echo hello world" ;
109- let mut parts = input. splitn ( 2 , ' ' ) ;
110- let command = parts. next ( ) . unwrap_or ( "" ) ;
111- let args = parts. next ( ) . unwrap_or ( "" ) ;
106+ // split and check first word
107+ let input = "echo hello world" ;
108+ let mut parts = input. splitn ( 2 , ' ' ) ;
109+ let command = parts. next ( ) . unwrap_or ( "" ) ;
110+ let args = parts. next ( ) . unwrap_or ( "" ) ;
112111
113- println ! ( "command: '{}', args: '{}'" , command, args) ;
112+ println ! ( "command: '{}', args: '{}'" , command, args) ;
114113
115- // PART 7: Case Conversion
114+ // PART 7: Case Conversion
116115
117- let s = "Hello Universe" ;
118- println ! ( "converted to lowercase: {}" , s. to_lowercase( ) ) ;
119- println ! ( "converted to uppercase: {}" , s. to_uppercase( ) ) ;
116+ let s = "Hello Universe" ;
117+ println ! ( "converted to lowercase: {}" , s. to_lowercase( ) ) ;
118+ println ! ( "converted to uppercase: {}" , s. to_uppercase( ) ) ;
120119
121- // PART 8: Parsing --> converting strings to other types
122- let num: i32 = "42" . parse ( ) . unwrap ( ) ;
123- println ! ( "parsed i32: {}" , num) ;
120+ // PART 8: Parsing --> converting strings to other types
121+ let num: i32 = "42" . parse ( ) . unwrap ( ) ;
122+ println ! ( "parsed i32: {}" , num) ;
124123
125- let float: f64 = "3.14" . parse ( ) . unwrap ( ) ;
126- println ! ( "parsed f64: {}" , float) ;
124+ let float: f64 = "3.14" . parse ( ) . unwrap ( ) ;
125+ println ! ( "parsed f64: {}" , float) ;
127126
128- // Handle parse failure safely
129- match "abc" . parse :: < i32 > ( ) {
130- Ok ( n) => println ! ( "parsed: {}" , n) ,
131- Err ( e) => println ! ( "parse failed: {}" , e) ,
132-
133- }
127+ // Handle parse failure safely
128+ match "abc" . parse :: < i32 > ( ) {
129+ Ok ( n) => println ! ( "parsed: {}" , n) ,
130+ Err ( e) => println ! ( "parse failed: {}" , e) ,
131+ }
134132
135- //number --> string
136- let n = 42 ;
137- let s = n. to_string ( ) ;
138- println ! ( "number to string: {}" , s) ;
133+ //number --> string
134+ let n = 42 ;
135+ let s = n. to_string ( ) ;
136+ println ! ( "number to string: {}" , s) ;
139137
140- // PART 9: Joining
138+ // PART 9: Joining
141139
142- let words = vec ! [ "It's" , "Rust Lang" , "baby" ] ;
140+ let words = vec ! [ "It's" , "Rust Lang" , "baby" ] ;
143141
144- // join --> connect items with a separator
145- let sentence = words. join ( " " ) ;
146- println ! ( "{}" , sentence) ;
142+ // join --> connect items with a separator
143+ let sentence = words. join ( " " ) ;
144+ println ! ( "{}" , sentence) ;
147145
148- let csv = words. join ( "," ) ;
149- println ! ( "{}" , csv) ;
146+ let csv = words. join ( "," ) ;
147+ println ! ( "{}" , csv) ;
150148
151- // PART 10: Characters
149+ // PART 10: Characters
152150
153- let s = String :: from ( "hello" ) ;
154- for i in s. chars ( ) {
155- print ! ( "{} " , i) ;
156- }
157- println ! ( ) ;
151+ let s = String :: from ( "hello" ) ;
152+ for i in s. chars ( ) {
153+ print ! ( "{} " , i) ;
154+ }
155+ println ! ( ) ;
158156
159- // Count characters
160- let char_count = s. chars ( ) . count ( ) ;
161- println ! ( "char count: {}" , char_count) ;
162-
157+ // Count characters
158+ let char_count = s. chars ( ) . count ( ) ;
159+ println ! ( "char count: {}" , char_count) ;
163160}
0 commit comments