0

What is the most rusty/current way in (latest Rust) to do a basic and efficient string slit using multiple different possible split characters, i.e. convert:

["name, age. location:city"]

into:

["name", "age", "location", "city"]

I understand that we could use a regexp to do this, but this code will be used in log processing and is used on a computer that is cpu/memory bound so I'd prefer to avoid regexp if possible.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Jay
  • 19,649
  • 38
  • 121
  • 184
  • 2
    Why is the outer array? Do you want to split exactly like that, or is this just an example? Do you want to split on all commas, periods and colons, or just on the first? Please be more specific. – Chayim Friedman Aug 30 '23 at 08:53
  • I've updated the question text with information from the question heading to make it clearer. – Jay Aug 30 '23 at 08:56

2 Answers2

3

str::split accepts a generic splitting Pattern, which can be one of various different things. In particular, it can be a closure deciding whether a character is a delimiter, thus giving us full control while allowing us to take advantage of helper functions in char.

let value = "name, age. location:city";
let parts: Vec<_> = value
    .split(|c: char| c.is_ascii_punctuation() || c.is_ascii_whitespace())
    .filter(|p| !p.is_empty())
    .collect();
assert_eq!(&parts[..], &["name", "age", "location", "city"]);

Playground

See also: How do I split a string in Rust?

E_net4
  • 27,810
  • 13
  • 101
  • 139
1

This will split using any a set of characters you choose and return an array of strings.

let items = value
    .split(&[' ', ',', ':', '-'])
    .filter(|&r| r != "")
    .map(|r| r.to_string() )
    .collect();

Or alternatively, for a regular string:

let items = value
    .split(&[' ', ',', ':', '-', '\t'])
    .filter(|&r| r != "").collect()

Rust Playground

Jay
  • 19,649
  • 38
  • 121
  • 184