rust-lang / rust-lang/rfcs

Don’t require explicitly initialising zero-sized types

Open
#896 11 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-lang
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

It’s annoying to have to initialise zero-sized types such as those in std::markers in struct expressions:

/// A reference type.
struct Foo<'a, T> {
    ptr: *const T,
    _marker: PhantomData<&'a T>,
}
let x = 1;
let y = Foo {
    ptr: &x,
    _marker: PhantomData,
};
// would be much nicer as
let y = Foo { ptr: &x };

This could also slightly improve the ergonomics of the hack in which one adds a private () field to a struct to stop it from being exhaustively matched on:

mod foo {
    pub struct Foo {
        pub a: u32,
        pub b: &'static str,
        _extra: (),
    }
    impl Foo {
        fn new(a: u32, b: &'static str) -> Foo {
            Foo { a: a, b: b } // no need to specify _extra
        }
    }
}
let foo = Foo::new(95, "hello");

This could potentially extend to let bindings:

let x: UnitStruct = UnitStruct;
// would be (perhaps) nicer as
let x: UnitStruct;
// although this isn’t particularly useful because one can already do
let x = UnitStruct;

although I don’t know how useful that would be. It would also have to respect privacy rules:

mod foo {
    pub struct Foo(());
}
// even though Foo is zero-sized, it has inaccessible fields and so
// cannot be implicitly constructed
let x: foo::Foo;
println!("{}", x); // error: use of possibly uninitialized value

This wouldn’t, however, work with empty types (enum Foo {}) because they have no valid values (and thus there is no way to construct them).

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No implementation files or tests are named. Start by reviewing the struct-expression examples in the issue, then define the proposed behavior for zero-sized fields, privacy, let bindings, and empty types. Done means the RFC has a settled, implementable specification for these cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.