user2665887
user2665887

Reputation: 923

What traits are implemented by unit type?

http://doc.rust-lang.org/std/ gives no explicit answer and has no separate page for () unlike for the other primitive types.

It looks like unit implements the same traits as tuples in general:
Clone
PartialEq
Eq
PartialOrd
Ord
Default
,
but at the same time unit is treated separately from tuples everywhere in the documentation.

Upvotes: 4

Views: 602

Answers (1)

huon
huon

Reputation: 102166

I'm just writing a page for () now: PR #15321... and now visible as std::unit::unit.

In the interim, Rust is quite greppable, and grepping for impl.*for *() turns up a pile of impls (however, it's not all of them, since some are generated by macros):

src/libcollections/hash/mod.rs:150:        impl<S: Writer> Hash<S> for () {
src/libcore/cmp.rs:211:    impl PartialEq for () {
src/libcore/cmp.rs:243:    impl PartialOrd for () {
src/libcore/cmp.rs:270:    impl Ord for () {
src/libcore/fmt/mod.rs:740:impl Show for () {
src/libdebug/repr.rs:39:impl Repr for () {
src/librand/rand_impls.rs:192:impl Rand for () {
src/librustc/util/ppaux.rs:509:impl Repr for () {
src/libserialize/json.rs:2209:impl ToJson for () {
src/libserialize/serialize.rs:361:impl<E, S:Encoder<E>> Encodable<S, E> for () {
src/libserialize/serialize.rs:367:impl<E, D:Decoder<E>> Decodable<D, E> for () {
src/libsyntax/ext/quote.rs:150:    impl ToSource for () {

(as well as a pile in tests.)

In summary, the traits of interest there are: Hash, PartialEq, PartialOrd, Ord, Show, Rand, ToJson, Encodable, Decodable. There's also at least Default, TotalEq, Clone via macros.

Upvotes: 1

Related Questions