|
| 1 | +fn main(){ |
| 2 | + |
| 3 | + // PART 1: &str vs String |
| 4 | + |
| 5 | + // &str --> fized text baked into your program |
| 6 | + let fixed: &str = "hello world"; |
| 7 | + println!("&str: {}", fixed); |
| 8 | + |
| 9 | + // String --> owned, growable txt |
| 10 | + let owned: String = String::from("hello world"); |
| 11 | + println!("String: {}", owned); |
| 12 | + |
| 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 | + |
| 18 | + // convert String --> &str |
| 19 | + let owned = String::from("hello"); |
| 20 | + let borrowed : &str = &owned; // borrow it as &str |
| 21 | + prontln!("borrowed: {}", borrowed); |
| 22 | + |
| 23 | + // PART 2: Building Strings |
| 24 | + |
| 25 | + // push_str --> appedn a &str |
| 26 | + let mut s =String::from("hello"); |
| 27 | + s.push_str(" Universe"); |
| 28 | + println!("{} ", s); |
| 29 | + |
| 30 | + // push --> append a single character |
| 31 | + s.push('!'); |
| 32 | + println!("{}", S); |
| 33 | + |
| 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 |
| 40 | + |
| 41 | + println!("{}", s3); |
| 42 | + |
| 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, second); |
| 47 | + |
| 48 | + println!("{}", combined); //NOTE: first & sec are still valid -- format! borrows them |
| 49 | + |
| 50 | + // repeat |
| 51 | + let repeated = "ha".repeat(3); |
| 52 | + println!("{}", repeated); |
| 53 | + |
| 54 | + // PART 3: Trimming |
| 55 | + let s = " Namaste India "; |
| 56 | + |
| 57 | + println!("'{}'", s.trim()); // 'Namaste India' |
| 58 | + println!("'{}'", s.trim_start()); |
| 59 | + println!("'{}'", s.trim_end()); |
| 60 | + |
| 61 | + // trim specific characters |
| 62 | + let s2 = "###hello###"; |
| 63 | + println!("{}", s2.trim_matches('#')); |
| 64 | + |
| 65 | + // PART 4: Searching & Checking |
| 66 | + |
| 67 | + let s = String::from("hello, rust world!!"); |
| 68 | + |
| 69 | + println!("contains 'Rust': {}", s.contains("Rust")); |
| 70 | + println!("starts_with 'Hello': {}", s.starts_with("Hello")); |
| 71 | + println!("ends_with 'World!': {}", s.ends_with("World!")); |
| 72 | + println!("is it empty? : {}", s.is_empty()); |
| 73 | + |
| 74 | + // find --> returns the index of first match, or None |
| 75 | + match s.find("Rust") { |
| 76 | + Some(i) => println!("'Rust' found at index {}", i); |
| 77 | + None => println!("not found"); |
| 78 | + } |
| 79 | + |
| 80 | + // PART 5 : Replacing |
| 81 | + |
| 82 | + let s = String::from("I love Java and Java was my first programming language"); |
| 83 | + |
| 84 | + // replace --> replaces ALL occurences |
| 85 | + |
| 86 | + |
| 87 | +} |
0 commit comments