Default generic type of `RawVec` in rust's std lib

For rust's standard library, there is RawVec<T, A> which is the backbone of Vec<T> defined as:

pub struct RawVec<T, A: Allocator = Global> {
    ptr: Unique<T>,
    cap: usize,
    alloc: A,

This struct takes Global as its default allocator type here.

The first implementation of RawVec:

impl<T> RawVec<T, Global> {
    pub const fn new() -> Self {

The second:

impl<T, A: Allocator> RawVec<T, A> {
    pub const fn new_in(alloc: A) -> Self {
        Self { ptr: Unique::dangling(), cap: 0, alloc }

The second implementation is more generic than the first one in that it tasks account of Allocator. The first implementation benefits from the second one, and uses Global as its allocator.

However, what I am wondering here is that Global is actually a type, not a variable, how does this statement works?


To me, this statement passes a type of Global as function parameter, and obviously not possible.

Global is a unit struct which implements the Allocator trait. To create a new Global value just Global is written instead of something like Global { ...blah ... } because it has no fields (or even braces).

Here's an example of another unit struct I called Empty:

struct Empty;

fn main() {
    let value = Empty;



The reason why that's allowed is because creating a unit struct also creates a constant of the same name. So pub struct Global; is equivalent to pub struct Global {} pub const Global: Global = Global {};

1 Like

@Kestrer @rschoon Both of your answers help a lot. Thanks.

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.