rustlings

solving rustlings ft. dracuxan
git clone [email protected]:dracuxan/rustlings.git
Log | Files | Refs

strings4.rs (1170B)


      1 fn string_slice(arg: &str) {
      2     println!("{arg}");
      3 }
      4 
      5 fn string(arg: String) {
      6     println!("{arg}");
      7 }
      8 
      9 fn main() {
     10     string_slice("blue");
     11 
     12     string("red".to_string());
     13 
     14     string(String::from("hi"));
     15 
     16     string("rust is fun!".to_owned());
     17 
     18     // Here, both answers work.
     19     // `.into()` converts a type into an expected type.
     20     // If it is called where `String` is expected, it will convert `&str` to `String`.
     21     string("nice weather".into());
     22     // But if it is called where `&str` is expected, then `&str` is kept `&str` since no conversion is needed.
     23     // If you remove the `#[allow(…)]` line, then Clippy will tell you to remove `.into()` below since it is a useless conversion.
     24     #[allow(clippy::useless_conversion)]
     25     string_slice("nice weather".into());
     26 
     27     string(format!("Interpolation {}", "Station"));
     28 
     29     // WARNING: This is byte indexing, not character indexing.
     30     // Character indexing can be done using `s.chars().nth(INDEX)`.
     31     string_slice(&String::from("abc")[0..1]);
     32 
     33     string_slice("  hello there ".trim());
     34 
     35     string("Happy Monday!".replace("Mon", "Tues"));
     36 
     37     string("mY sHiFt KeY iS sTiCkY".to_lowercase());
     38 }