Answers for "rust get nth char in string"

0

rust get nth char in string

If you want just the first char, then don't collect into a Vec<char>, just use the iterator:

let text = "hello world!";
let ch = text.chars().next().unwrap();

Alternatively, you can use the iterator's nth method:

let ch = text.chars().nth(0).unwrap();
Posted by: Guest on February-16-2021

Browse Popular Code Answers by Language