Several questions on the String



  1. How to get a particular character of String? String is not indexable.
  2. How to get reverse string? There is a method in the standard library?


Here are the docs for String:

docs on string indexing, and why it is not simple: (spoiler: because it is not simple in real life)

If you want to index, you can get the bytes and index those:

fn main() {
    let b = "bors".as_bytes();
    println!("{:?}", b[0] as char);


and here is reversal:

fn main() {
    let b = "bors";
    let rev: String = b.chars().rev().collect();
    println!("{:?}", rev);





One thing to take away from this: Strings are UTF-8, and ‘character’ isn’t a thing in UTF-8. You have bytes, codepoints, and grapheme clusters. will give you iterators over the last ones.