rust - What is the difference between using a type as a different name and a type alias? -
what difference between
use hyper::status::statuscode error;
and
type error = hyper::status::statuscode;
are more differences between them except type
can pub type
? benefits between using 1 or another?
in case of simple types, in example, there doesn't seem semantic difference. moreover, there direct analogue use
pub type
, it's pub use
:
// available other modules pub use hyper::status::statuscode error;
however, there differences in more complex cases. example, can define generic type aliases or aliases specialized generic types:
type result<t> = ::std::result::result<t, myerror>; type optioni32 = option<i32>;
the general idea use type aliases because more powerful , suggest intent more clearly, result
, , use use .. ..
when want import specific name conflicts in current namespace:
use std::io::read stdread; trait read: stdread { ... }
note using path-qualified identifiers should preferred use
renaming. above better written as
use std::io; trait read: io::read { ... }
(unless read
methods used concrete type in same file, of course).
using use .. ..
substitute type
(in case possible) uncommon , think should avoided.
Comments
Post a Comment