What are the differences between the multiple ways to create zero-sized structs?

寵の児 提交于 2020-12-02 05:54:27

问题


I found four different ways to create a struct with no data:

  • struct A{} // empty struct / empty braced struct
    
  • struct B(); // empty tuple struct
    
  • struct C(()); // unit-valued tuple struct
    
  • struct D; // unit struct
    

(I'm leaving arbitrarily nested tuples that contain only ()s and single-variant enum declarations out of the question, as I understand why those shouldn't be used).

What are the differences between these four declarations? Would I use them for specific purposes, or are they interchangeable?

The book and the reference were surprisingly unhelpful. I did find this accepted RFC (clarified_adt_kinds) which goes into the differences a bit, namely that the unit struct also declares a constant value D and that the tuple structs also declare constructors B() and C(_: ()). However it doesn't offer a design guideline on why to use which.

My guess would be that when I export them with pub, there are differences in which kinds can actually be constructed outside of my module, but I found no conclusive documentation about that.


回答1:


There are only two functional differences between these four definitions (and a fifth possibility I'll mention in a minute):

  1. Syntax (the most obvious). mcarton's answer goes into more detail.
  2. When the struct is marked pub, whether its constructor (also called struct literal syntax) is usable outside the module it's defined in.

The only one of your examples that is not directly constructible from outside the current module is C. If you try to do this, you will get an error:

mod stuff {
    pub struct C(());
}
let _c = stuff::C(());  // error[E0603]: tuple struct `C` is private

This happens because the field is not marked pub; if you declare C as pub struct C(pub ()), the error goes away.

There's another possibility you didn't mention that gives a marginally more descriptive error message: a normal struct, with a zero-sized non-pub member.

mod stuff {
    pub struct E {
        _dummy: (),
    }
}
let _e = stuff::E { _dummy: () };  // error[E0451]: field `_dummy` of struct `main::stuff::E` is private

(Again, you can make the _dummy field available outside of the module by declaring it with pub.)

Since E's constructor is only usable inside the stuff module, stuff has exclusive control over when and how values of E are created. Many structs in the standard library take advantage of this, like Box (to take an obvious example). Zero-sized types work in exactly the same way; in fact, from outside the module it's defined in, the only way you would know that an opaque type is zero-sized is by calling mem::size_of.

See also

  • What is an idiomatic way to create a zero-sized struct that can't be instantiated outside its crate?
  • Why define a struct with single private field of unit type?



回答2:


struct D; // unit struct

This is the usual way for people to write a zero-sized struct.

struct A{} // empty struct / empty braced struct
struct B(); // empty tuple struct

These are just special cases of basic struct and tuple struct which happen to have no parameters. RFC 1506 explains the rational to allow those (they didn't used to):

Permit tuple structs and tuple variants with 0 fields. This restriction is artificial and can be lifted trivially. Macro writers dealing with tuple structs/variants will be happy to get rid of this one special case.

As such, they could easily be generated by macros, but people will rarely write those on their own.

struct C(()); // unit-valued tuple struct

This is another special case of tuple struct. In Rust, () is a type just like any other type, so struct C(()); isn't much different from struct E(u32);. While the type itself isn't very useful, forbidding it would make yet another special case that would need to be handled in macros or generics (struct F<T>(T) can of course be instantiated as F<()>).

Note that there are many other ways to have empty types in Rust. Eg. it is possible to have a function return Result<(), !> to indicate that it doesn't produce a value, and cannot fail. While you might think that returning () in that case would be better, you might have to do that if you implement a trait that dictates you to return Result<T, E> but lets you choose T = () and E = !.



来源:https://stackoverflow.com/questions/50162597/what-are-the-differences-between-the-multiple-ways-to-create-zero-sized-structs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!