Skip to content

Instantly share code, notes, and snippets.

@csknk
Forked from rust-play/playground.rs
Last active August 19, 2020 20:33
Show Gist options
  • Save csknk/bf3cce9426cedec6d3ce9758dcbed382 to your computer and use it in GitHub Desktop.
Save csknk/bf3cce9426cedec6d3ce9758dcbed382 to your computer and use it in GitHub Desktop.

Revisions

  1. csknk revised this gist Aug 19, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion returning-strings-from-function.rs
    Original file line number Diff line number Diff line change
    @@ -12,6 +12,6 @@ fn pluralize(s: String) -> String {
    p.push_str("s");
    return p

    // This is ok
    // This is ok - builds new String, which is moved out
    // s + "s"
    }
  2. csknk renamed this gist Aug 19, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @rust-play rust-play created this gist Aug 19, 2020.
    17 changes: 17 additions & 0 deletions playground.rs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    fn main() {
    let s = String::from("book");

    let ss = pluralize(s.clone());
    println!("One {}, many {}", s, ss);
    }

    fn pluralize(s: String) -> String {

    // Can't push_str directly onto s as s is not mutable
    let mut p = s;
    p.push_str("s");
    return p

    // This is ok
    // s + "s"
    }