One solution is to use chars
method. This return an iterator over the char
s of a string slice.
let letter_as_char: char = letter.chars().next().unwrap();
println!("{:?}", letter_as_char);
But it is important to remember that
char
represents a Unicode Scalar Value, and might not match your idea
of what a ‘character’ is. Iteration over grapheme clusters may be what
you actually want. For example, Consider the String H
let y = "H";
let mut chars = y.chars();
assert_eq!(Some('H'), chars.next());
assert_eq!(None, chars.next());
Now consider "y̆"
let y = "y̆";
let mut chars = y.chars();
assert_eq!(Some('y'), chars.next()); // not 'y̆'
assert_eq!(Some('\u{0306}'), chars.next());
assert_eq!(None, chars.next());
See also: