I will try to answer directly to your question, then I will comment some points.
str::Split
is an Iterator
, so you need to collect
from it into a Vec
(in this case). You also did not specify if your function takes a Vec<String>
or a Vec<&str>
. str::Split::collect
produces str
(a sized reference to a contiguous UTF-8 encoded bytes), and if you need the former then you have to do something like
let vec_club: Vec<_> = arg_club.map(|s| s.to_string()).collect();
and you can pass this to the function.
See if this already helps. Now I want to show you a couple of things.
It looks like arg_two
is a str
, are you sure you need to create an owned String
with to_string()
?
If you have the possibility, it is probably better to change the signature of process_line
in order to take str
s and slices of str
(or, even better in your case, an Iterator
). Here what I mean using slices and str
s:
use std::env;
fn main() {
let arg_two = env::args()
.skip(2)
.next()
.expect("I need at least two arguments!");
let arg_club: Vec<_> = arg_two.split("/").collect();
let _ = process_line(&arg_two, 0, &arg_club);
}
pub fn process_line<'a>(
in_line: &'a str,
in_date: i32,
in_clubs: &[&'a str],
) -> (i32, &'a str) {
// Just for example
(in_date, in_clubs[2])
}
In this case I told the compiler that in_line
, the str
s in the slice in_clubs
and the str
you are getting as output have the same lifetime.
An interesting approach in this case could also be something like
use std::env;
fn main() {
let arg_two = env::args()
.skip(2)
.next()
.expect("I need at least two arguments!");
let _ = process_line(&arg_two, 0, arg_two.split("/"));
}
pub fn process_line<'a, Iter>(
in_line: &'a str,
in_date: i32,
in_clubs: Iter,
) -> (i32, &'a str)
where
Iter: Iterator<Item = &'a str>
{
// Just for example, don't do that!
(in_date, in_clubs.skip(2).next().unwrap())
}
In this case the function is taking an object impl
ementing Iterator
and that returns a reference to str
with the same lifetime of in_line
.
Maybe your case can be different from what I showed you, but on the other hand I hope these examples can be useful.