rust - `use` statement necessary for trait not used directly in this source file: why? -
in code below, removing second line result in compilation error saying:
type `std::io::net::tcp::tcplistener` not implement method in scope named `listen`
since directly using listener (even though std
uses internally), why need specify it?
use std::io::{tcplistener, tcpstream}; use std::io::{acceptor, listener}; fn handle_client(mut stream: tcpstream) { // ... } fn main() { let args = std::os::args(); println!("{}", args); let listener = tcplistener::bind("127.0.0.1", 80).unwrap(); let mut acceptor = listener.listen().unwrap(); stream in acceptor.incoming() { spawn(proc() { handle_client(stream.unwrap()); }); } }
it design decision of language require explicit use
of traits implement methods. example in following code:
use my::a; use my::b; mod { pub trait { fn foo(&self); } pub struct b; impl b { pub fn bar(&self) { println!("called `bar`."); } } impl b { fn foo(&self) { println!("called `foo`."); } } } fn main() { // requires "use my::b". let b = b; b.bar(); // requires "use my::a". b.foo(); }
i believe motivation heavily due fact there no seamless way support multiple traits same method name. there lot of work being done traits atm https://github.com/rust-lang/rfcs/blob/master/active/0024-traits.md.
Comments
Post a Comment