In Rust, what's the idiomatic way to split a &str into an iterator of &strs of one character each?

rust language
rust tutorial
rust map
how to start in rust 2020
rust guide
rust basics
getting started in rust 2020
how to play rust 2020

If I want to take a &str like "aeiou" and turn it into an iterator roughly equivalent to ["a", "e", "i", "o", "u"].iter(), what's the most idiomatic way to do it?

I've tried doing "aeiou".split("") which seemed idiomatic to me, but I got empty &strs at the beginning and end.

I've tried doing "aeiou".chars() but it got pretty ugly and unwieldy from there trying to turn the chars into &strs.

For the time being, I just typed out ["a", "e", "i", "o", "u"].iter(), but there's got to be an easier, more idiomatic way.

For context, I'm eventually going to be looping over each value and passing it into something like string.matches(vowel).count().

Here's my overall code. Maybe I went astray somewhere else.

fn string_list_item_count<'a, I>(string: &str, list: I) -> usize
where
    I: IntoIterator<Item = &'a str>,
{
    let mut num_instances = 0;

    for item in list {
        num_instances += string.matches(item).count();
    }

    num_instances
}

// snip

string_list_item_count(string, vec!["a", "e", "i", "o", "u"])

// snip

If I could make string_list_item_count accept the std::str::pattern::Pattern trait inside the iterator, I think that would make this function accept iterators of &str and char, but the Pattern trait is a nightly unstable API and I'm trying to avoid using those.

You can use split_terminator instead of split to skip the empty string at the end of the iterator. Additionally, if you skip the first element of the iterator, you get the result you want:

let iterator = "aeiou".split_terminator("").skip(1);
println!("{:?}", iterator.collect::<Vec<_>>());

Output:

["a", "e", "i", "o", "u"]

Data Types, Every value in Rust is of a certain data type, which tells Rust what kind of data is being specified so it knows how to work with that data. We'll look at two data� Rust is the common name for iron oxide. The most familiar form of rust is the reddish coating that forms flakes on iron and steel (Fe 2 O 3), but rust also comes in other colors including yellow, brown, orange, and even green! The different colors reflect various chemical compositions of rust.

I would use str::split with an empty string and then remove any empty strings using Iterator::filter:

fn string_chars(s: &str) -> impl Iterator<Item = &str> {
    s.split("").filter(|s| !s.is_empty())
}

fn main() {
    assert_eq!(
        string_chars("aeiou").collect::<Vec<_>>(),
        ["a", "e", "i", "o", "u"],
    );
}

How to Get Started, INFO↓ In this Blootorial, I show you how to get started and what servers to choose to do so Duration: 10:43 Posted: 15 Jan 2019 Rust definition, the red or orange coating that forms on the surface of iron when exposed to air and moisture, consisting chiefly of ferric hydroxide and ferric oxide formed by oxidation.

A closure can also serve as a Pattern:

fn main() {
    let vowels = "aeiou";
    let s = "the quick brown fox jumps over the lazy dog";
    let count = string_item_count(s, vowels);
    dbg!(count);
}

fn string_item_count(string: &str, pat: &str) -> usize {
    let pred = |c| pat.contains(c);
    string.matches(pred).count()
}

Getting started in Rust — Rustafied, A guide on how to get started in Rust Experimental. It covers the everything you need to know to build your first base in Rust. Rust is a multi-paradigm programming language focused on performance and safety, especially safe concurrency. Rust is syntactically similar to C++, and provides memory safety without using garbage collection. Rust was originally designed by Graydon Hoare at Mozilla Research, with contributions from Dave Herman, Brendan Eich, and others.

What is Rust and why is it so popular?, I found this and this and this and this that explains my question. The 'a reads 'the lifetime a'. Technically, every reference has some lifetime associated with it, but� Rust is an iron oxide, a usually reddish brown oxide formed by the reaction of iron and oxygen in the presence of water or air moisture. Several forms of rust are distinguishable both visually and by spectroscopy, and form under different circumstances.

Rust (video game), What is Rust? Rust is an open-source systems programming language that focuses on speed Duration: 5:28 Posted: 9 Jun 2017 ↓INFO↓ In this Blootorial, I show you how to get started and what servers to choose to do so. There's both the standard start for new players and the snowball start for experienced players. I

Rust (programming language), Although it's only an alpha build, I already feel like I've gotten my money's worth from Rust, having spent many hours trying to survive the� A Status Effect, also referred to as "buff" or "debuff", is an effect which alters your character's status. There are both, negative and positive status effects, however most status effects in Rust are negative. They are displayed in the right lower corner of the GUI and depending on how they affect the character, are either orange/red or green, sometimes followed by a descriptive number or

Comments
  • Does this answer your question? How do I convert a string to a list of chars?
  • No, I'm looking to basically do the opposite. Turn an iterator of chars into an iterator of &strs.
  • How do you define character in the context of your question? ASCII character? Unicode codepoint? Grapheme cluster? ...?
  • That's a good point. And it's why I wish Pattern was stable so I didn't have to specify exactly one type. I wish I could use anything that .matches accepts. It's kind of odd to me that .matches is stable but its underlying trait is not.
  • Huh. That's interesting. It still feels a bit bad just because it's more characters than just typing out the vec. 😉
  • Yes. But it does scale better for longer strings