I'm learning Rust and while working on a small program came across this quirk with implementing traits.
I have a small one method trait that takes a string and returns a struct.
pub trait Deserialize {
fn deserialize(&str) -> Self;
}
When I implement it for a simple struct Point{lat: f64, long: f64}
impl Deserialize for Point {
fn deserialize(&str) -> Point {
...
}
}
I get an error "the size for values of type Self
cannot be known at compilation time". I can get around it by changing the signature to fn deserialize(&str) -> Box<Self>.
However looking through some examples of Rust code I saw the following which works
pub trait Deserialize<T> {
fn deserialize(&str) -> T {
...
}
}
impl Deserilize<Point> for Point {
fn deserialize(&str) -> Point {
...
}
}
I do not get the error and do not need to Box the result. Is there some disadvantage of using the second method for defining and implementing the trait? The first method using Self seems to be the one that is preferred in the documentation.