I was helping a friend learn Rust by going through Advent of Code 2020, and they needed to split a &str into a Vec<char>. They found the str::split method, and wrote string.split("").collect::<Vec<char>>(). While reviewing their code, this tripped me up a bit, since I would think of string.chars().collect::<Vec<char>>() instead. I think this would be a good candidate for a lint.
Advantage
I think this is more intuitive than splitting with an empty pattern
Drawbacks
as this playground shows, this is not a one-to-one replacement because str::split("") has leading and trailing empty strings.
Example
"hello world".split("");
The above should trigger a suggestion of replacing with
What it does
I was helping a friend learn Rust by going through Advent of Code 2020, and they needed to split a
&str
into aVec<char>
. They found thestr::split
method, and wrotestring.split("").collect::<Vec<char>>()
. While reviewing their code, this tripped me up a bit, since I would think ofstring.chars().collect::<Vec<char>>()
instead. I think this would be a good candidate for a lint.Advantage
Drawbacks
str::split("")
has leading and trailing empty strings.Example
The above should trigger a suggestion of replacing with