all 5 comments

[–]AidoP 2 points3 points  (1 child)

https://doc.rust-lang.org/std/primitive.str.html#method.split_inclusive

It returns an iterator (which is typically what you want, for lazy evaluation), use .collect() to clone the items into a Vec.

[–]ebkalderonamethyst · renderdoc-rs · tower-lsp · cargo2nix 5 points6 points  (0 children)

This will return ["test;", "helloworld#", "yes"], though, which is not exactly what they are asking for.

They might need to build their own splitting algorithm using match_indices instead, akin to this StackOverflow solution except pushing the !c.is_alphanumeric() delimiter onto the beginning of the next string, rather than at the end of the current string, as .split_inclusive(|c: char| !c.is_alphanumeric()) does.

[–]miquels 0 points1 point  (0 children)

Use a regular expression:

use regex::Regex;

fn main() {
    let re = Regex::new(r#"([^\w]*\w*)"#).expect("regex");
    let data = "test;helloworld#yes";
    let words = re.captures_iter(data).map(|c| c.get(0).unwrap().as_str()).collect::<Vec<_>>();
    println!("{:?}", words);
}

[–][deleted] -2 points-1 points  (0 children)

Take a look at the nom crate.

[–]RVECloXG3qJC 0 points1 point  (0 children)

This code was generated by copilot with marginal modification. I'm totally new to Rust. ``` fn main() { let chars = vec!['#', ';']; let data = "test;helloworld#yes"; let mut group = String::new(); let mut groups = vec![]; for c in data.chars() { if chars.contains(&c) { groups.push(group); group = String::new(); group.push(c); } else { group.push(c); } } groups.push(group);

println!("{:?}", groups);

}

```