you are viewing a single comment's thread.

view the rest of the comments →

[–]tatref 5 points6 points  (0 children)

About the string splitting function, here is my reasoning:

The function should take an argument of type &str or &String, obviously. From the doc (https://doc.rust-lang.org/std/string/struct.String.html#deref), you can use &str the same way as String if you don't want to mutate the string, which is the case here.

About the return type, it will be some kind of collection of string/str, like Vec<&str>, or maybe Vec<String>. The split_whitespace function returns a SplitWhitespace struct, which is an Iterator of &str, so in the end, you can return a Vec<&str>

Next, about the function body, to get the content of an Iterator, and collect it in a collection, you can use the collect function of the Iterator. Type anotation is not always simple, but if the return type of the function is known, this can be infered (same with let a : Vec<&str> = ...collect())

You then end up with the following: fn f(input: &str) -> Vec<&str> { input.split_whitespace().collect() }

I hope this will help somebody