9951 explained code solutions for 126 technologies


rustHow do I get a character from a string in Rust by its index?


You can get a character from a string in Rust by its index using the chars() method. This method returns an iterator over the characters of a string.

Example code

let s = "Hello";
let c = s.chars().nth(1);

Output example

Some('e')

Code explanation

  • let s = "Hello": This declares a string variable s and assigns it the value "Hello".
  • let c = s.chars().nth(1): This uses the chars() method to get an iterator over the characters of the string s. The nth(1) method is then used to get the character at the index 1 from the iterator.

Helpful links

Edit this code on GitHub