我可以像这样分割&str
:
let v: Vec<&str> = "Mary had a little lamb".split(' ').collect();
assert_eq!(v, ["Mary", "had", "a", "little", "lamb"]);
有没有办法在保留迭代器中拆分的元素的同时拆分&str
?例如:
let v: Vec<&str> = "Mary had a little lamb".split_retain(' ').collect();
assert_eq!(v, ["Mary", " ", "had", " ", "a", " ", "little", " ", "lamb"]);
当我们只分割单个iter
时,此char
不是很有用,但当我们进行更复杂的拆分时,它会变得非常有用,例如:
let mut iter = some_str.split(|c: char| c.is_whitespace() || c == ':' || c == ',' || c == '(' || c == ')')