use std::net::TcpListener; use std::io::{Read,Write};
fn main(){ let listener = TcpListener::bind("127.0.0.1:4000").unwrap(); println!("Running on port 4000....");
// let result = listener.accept().unwrap();
for stream in listener.incoming(){
let mut stream = stream.unwrap();
println!("connection established");
let mut buffer = [0; 1024];
stream.read(&mut buffer).unwrap();
}
} 为什么非要 use std::io::{Read,Write};显式地引入 Trait 呢? impl trait Read 的代码不是在 TcpListener 里已经有了吗?
1
serco 2023-02-04 18:32:33 +08:00 1
不同的 trait 可以定义同名 method ,所以需要引入
|
2
beloved70020 2023-02-04 19:42:51 +08:00 via Android
因为你也可以在自己的库里为其实现自己的 Read
|
3
RiverRay 2023-02-05 11:10:52 +08:00 1
The reasoning for this is that multiple traits can define methods with the same name/signature.If this rule was not in place and there were multiple traits defining get for Read,
Except for fully qualifying the method call, but that would also require you to import the trait! |
4
NetLauu 2023-06-13 12:29:58 +08:00
the
|