I'm trying to implement From<&[&str]> for one of my types. It should consume an arbitrary length of lines (only known during runtime) and fetches data from the lines. The compiler complains about that &[&str; 2] is not &[&str], i.e. it can't convert a fixed-size slice to one with arbitrary length. The exact message is: the trait 'From<&[&str; 2]>' is not implemented for 'LineEater'
This is my attempt:
fn main() {
let data = ["foo", "bar"];
// works
foo(&data);
// doesn't work
LineEater::from(&data);
// also doesn't work
let data: &[&str] = data.into();
LineEater::from(&data);
}
struct LineEater;
impl From<&[&str]> for LineEater {
fn from(_lines: &[&str]) -> Self {
todo!()
}
}
fn foo(_slice: &[&str]) {}
It's confusing that this works with the call of foo() but not the call of from. In the first case Rust can "cast" the [&str; 2] to &[&str] but not in the latter. Why is this so and how can I fix it?
I'm working with Rust 1.55-nightly