Skip to content

Instantly share code, notes, and snippets.

@gembin
Last active May 5, 2022 05:35
Show Gist options
  • Save gembin/7b9b16d19568aedfebf8af35d7238b15 to your computer and use it in GitHub Desktop.
Save gembin/7b9b16d19568aedfebf8af35d7238b15 to your computer and use it in GitHub Desktop.

Revisions

  1. gembin revised this gist May 5, 2022. 1 changed file with 37 additions and 1 deletion.
    38 changes: 37 additions & 1 deletion rust_code_snippets.md
    Original file line number Diff line number Diff line change
    @@ -2,4 +2,40 @@

    ```rust
    let v: Vec<&str> = v.iter().map(AsRef::as_ref).collect();
    ```
    ```

    ### Convert a collection of characters into a String

    1. chars.iter().collect::<String>()


    ```rust
    // Vec<char> -> String
    let chars = vec!['a', 'b', 'c'];
    let string = chars.iter().collect::<String>();

    // [char] -> String
    let chars = ['a', 'b', 'c'];
    let string = chars.iter().collect::<String>();

    // &[char] -> String
    let slice_chars = &chars[..];
    let string = slice_chars.iter().collect::<String>();
    ```

    2. chars.concat()

    ```rust
    // Vec<char> -> String
    let chars = vec!["a", "b", "c"];
    let string = chars.concat();
    ```

    3. strings.join(separator)

    ```rust
    let names = ["firstName", "lastName"];
    let joined = names.join(", ");
    ```


  2. gembin created this gist May 5, 2022.
    5 changes: 5 additions & 0 deletions rust_code_snippets.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    ### Convert a Vec<String> to Vec<&str>

    ```rust
    let v: Vec<&str> = v.iter().map(AsRef::as_ref).collect();
    ```