保留元素分裂

时间:2017-12-16 17:20:16

标签: string split rust

我可以像这样分割&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 == ')')

0 个答案:

没有答案