Why does struct have two unsized fields?

Why does this code not compile?

enum Enumeration {

struct NotZeroSized {
    enumerated: Enumeration,
    name: str,

I get

error[E0690]: transparent struct needs at most one non-zero-sized field, but has 2
 --> src/lib.rs:7:1
7 | struct NotZeroSized {
  | ^^^^^^^^^^^^^^^^^^^ needs at most one non-zero-sized field, but has 2
8 |     enumerated: Enumeration,
  |     ----------------------- this field is non-zero-sized
9 |     name: str,
  |     --------- this field is non-zero-sized

For more information about this error, try `rustc --explain E0690`.
error: could not compile `playground` due to previous error

but str is not zero sized no?

#[repr(transparent)] can only be applied to types with a single value inside of them. The error is saying you can only have one field with size > 0, and you have two fields with size > 0


If str were zero-sized, you could use #[repr(transparent)]. But it is not zero-sized; it is not Sized at all. That is, the type does not have a statically known size, it has a dynamic size. The size of a str value is the number of bytes it contains, and that varies from str to str.


This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.