take this simple program.
use std::io;
fn main() {
<io::Stdout as io::Write>::write(&mut io::stdout(), b"hello").unwrap();
}
this form is actually useful in niche situations, such as if you already have a different Write
trait in scope.
take this simple program.
use std::io;
fn main() {
<io::Stdout as io::Write>::write(&mut io::stdout(), b"hello").unwrap();
}
this form is actually useful in niche situations, such as if you already have a different Write
trait in scope.
I think even just this will work
use std::io;
fn main() {
io::Write::write(&mut io::stdout(), b"hello").unwrap();
}
indeed it does, the longer form isn't required for items callable as methods since rust can look at the type of the self
argument to know what definition to dispatch on.
io::Write
this expression brings the trait into the scope for the expression.
it does not. io::Write::write(&mut io::stdout(), { io::stdout().write(b"foo"); b"bar" })
will fail to compile due to Write
not being in scope.
I meant more in the expression that evaluates to which function to call, not inside the whole expression. to me the usage OP showed still counts as putting the trait in-scope
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.