Can unit-like struct type be used as values directly?

struct A;          // Concrete type `A`.
struct S(A);       // Concrete type `S`.
struct SGen<T>(T); // Generic type `SGen`.

fn reg_fn(_s: S) {}

fn gen_spec_t(_s: SGen<A>) {}

fn gen_spec_i32(_s: SGen<i32>) {}

fn generic<T>(_s: SGen<T>) {}

fn main() {
    // Using the non-generic functions
    reg_fn(S(A));          // Concrete type.
    gen_spec_t(SGen(A));   // Implicitly specified type parameter `A`.
    gen_spec_i32(SGen(6)); // Implicitly specified type parameter `i32`.

    // Explicitly specified type parameter `char` to `generic()`.
    generic::<char>(SGen('a'));

    // Implicitly specified type parameter `char` to `generic()`.
    generic(SGen('c'));
}

I'm confused by reg_fn(S(A));

Shouldn't it be like this

let a = A;
let b = S(a);
reg_fn(b);

I mean , S(A) is a type, but used as value here. It's confusing

I think of it more like the struct initialization syntax ist just the same as the type name. It's just a struct initialization without the need for {} or (). Here the section from the book about unit-like structs:

https://doc.rust-lang.org/book/ch05-01-defining-structs.html#unit-like-structs-without-any-fields

2 Likes

A unit struct introduces a constant with the same name as the type, so:

struct A;

is equivalent to:

struct A {}
const A: A = A {};
3 Likes

Could you post the link to the official material for information on this matter?
Thank you

https://doc.rust-lang.org/nightly/reference/items/structs.html

1 Like

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.