Consider the following code:
use std::net::IpAddr;
pub struct Server {
host: IpAddr
}
impl Server {
fn new(host: IpAddr) -> Self {
Self {host}
}
}
fn main() {
let host = "192.168.0.1".parse().unwrap();
let server = Server::new(host.clone());
}
rustc complains that it cannot infer the type of host, and that type annotations are needed in its definition.
Due to the method signature of Server::new(), (fn new(host: IpAddr) -> Self), Rust can conclude that the type of the expression host.clone() must be IpAddr. I can understand that rustc can't be certain of the type of host based on this alone because any type can implement a method named clone() returning an IpAddr, but given that rustc can't find any other types that implement a clone() method which returns IpAddr, is it a bridge too far for rustc to assume that that host is a type that implements std::clone::Clone, and returns an IpAddr and therefore must be an IpAddr itself? What is the danger of such assumption that motivated the design of rustc to fail to compile this?