How do I convert a string to a list of chars?

Sarien :

Since a string supports iteration but not indexing, I would like to convert a string into a list of chars. I have "abc" and I want ['a', 'b', 'c'].

It can be any type as long as I can index into it. A Vec<char> or a [char; 3] would be fine, other ideas would also be interesting!

Faster would be better since I am dealing with very long strings. A version that is more efficient when assuming that the string is ASCII would also be cool.

Netwave :

Use the chars method on String or str:

fn main() {
    let s = "Hello world!";
    let char_vec: Vec<char> = s.chars().collect();
    for c in char_vec {
        println!("{}", c);
    }
}

Here you have a live example

Collected from the Internet

Please contact javaer1[email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I do it faster in java? Copy chars of a long string

How do I convert a C# List<string[]> to a Javascript array?

How do I convert a list into a string with spaces in Python?

How do I convert a list of ascii values to a string in python?

How do I convert an array list to hash separated string?

How to convert iterator of chars to String?

How do I convert a list of chars to a string in purescript

MySQL: How do I search and replace chars at the beginning of a string

How do I convert a string to a list of Maybe Int

How do I convert a TreeMap that is a <String, <List<String>> to String[][]?

How to convert a string type to an array of chars

How do I convert a list of numbers to a string in k?

How do i convert a String to a List<Map<String,String>>

how do I convert a list of custom type to a list of String

How do i remove chars from a string?

How do I convert special chars of passwords typed with CAPSLOCK?

How do I convert csv string to list in pandas?

How do I json_decode string with special chars (" \\ " )

How do I concat a string of chars together from a matrix in python?

How do I convert a list represented as a string to a list?

How do I convert a string of the form 'YYYY-DD-MM HH:MI:SS.MS +00:00' where last four chars are a timezone offset into a postgres timestamptz?

How do I convert a list of integers to a string - Python

How do I collect chars into a string in C?

How do I sort a list of chars based on how often they appear in a string?

How do I convert List<String?> to List<String> in .NET 6 / C# 10?

How to convert a string into a multidimensional array of chars

How do I interpret ASCII values of characters of a string as chars in C?

How do I remove some chars at the end of a string?

How can I convert a List<String> to String []?