Description
Failing Code
Consider the following code:
use std::net::TcpStream;
fn main() {
let stream = TcpStream::new();
}
This fails with an error that simply states that new
is not available:
Compiling playground v0.0.1 (/playground)
error[E0599]: no function or associated item named `new` found for type `std::net::TcpStream` in the current scope
--> src/main.rs:4:28
|
4 | let stream = TcpStream::new();
| ^^^ function or associated item not found in `std::net::TcpStream`
error: aborting due to previous error
Working Code
The intent is fairly clear: we want to construct a new TcpStream
using a method that follows the Rust naming conventions for constructors, but that method is not available. Instead the solution is likely to be either TcpStream::connect
or
TcpStream::connect_timeout
:
use std::net::TcpStream;
fn main() {
let stream = TcpStream::connect("localhost:8080");
}
Diagnostics Suggestions
When new
is not available it'd be ideal if the compiler could suggest
alternatives. A first heuristic for which methods to suggest be methods on
the same struct that don't take any self
params, have Self
as their
return type, and aren't implemented through any trait.
For TcpStream
this would include TcpStream::connect
and
TcpStream::connect_timeout
. But not TcpStream::try_clone
,
TcpStream::from_raw_fd
, TcpStream::from_raw_socket
and
std::net::Incoming::next
.
I'm not too sure how to structure the help message exactly, but I could
imagine something along these lines might work:
Compiling playground v0.0.1 (/playground)
error[E0599]: no function or associated item named `new` found for type `std::net::TcpStream` in the current scope
--> src/main.rs:4:28
|
4 | let stream = TcpStream::new();
| ^^^ function or associated item not found in `std::net::TcpStream`
= help: Did you mean to use `std::net::TcpStream::connect` or `std::net::TcpStream::connect_timeout`?
error: aborting due to previous error
cc/ @estebank