Help on *const ()

let mut x:*const ();
x=&1234;

If i want to chang integer pointer to float it dosent compile..

x = &44.4;

  1. () type is general type or tuple type?
    2.how to assign any type to x.

If you are using raw pointers, then you can cast between different pointer types. Generally all variables must have the same type for its entire lifetime.

if you box the variable you can use the Any trait, but in most cases that's a sign of poor design.

https://xyproblem.info/

It is not general -- it is specifically the unit type. You can think of it like an empty tuple, but it has other properties too.

1 Like

It looks like you are coming from C and you expect *const () to be the equivalent of void *. This expectation of yours is not going to be fulfilled on three levels:

  • Low-level primitive constructs have very different semantics in C and in Rust. Rust has a lot of constraints and a richer type system even around raw pointers. The languages also have radically different idioms and approaches – trying to directly map concepts from C to Rust is not going to help you, it would only cause endless grief.
  • () in Rust is not the same as C's void type. In Rust's real, algebraic type system, () is a proper type, the empty tuple (product) type, which is the canonical singleton type with exactly one possible value (also spelled ()) and thus no information content. Similarly to a void-returning C function, Rust functions that can't return any other meaningful value return (). However, unlike (), the C void "type" is not really a type. It's a keyword that has different meanings in different contexts.
  • Consequently, the void * type in C is not really first-class, its behavior is inconsistent with that of other pointer types: it's not really typed (you can assign any pointer type to a value of type void * and vice versa), and it can't be dereferenced (as it doesn't point to any particular type). In contrast, Rust's () type does compose with pointers, so *const () is a proper type, which points and dereferences to a – useless and zero-sized, but valid – value of type (), and you can't just assign a pointer-to-any-other-type to a variable with a declared type of *const (), at least not without an explicit conversion.

At this point I have to ask: what are you trying to do by assigning either a pointer-to-int or a pointer-to-float to the same variable? What higher-level goal do you have?

  • If you are trying to do dynamic dispatch, you can create a trait object (e.g. &dyn Display).
  • If you are trying to emulate a tagged union, you can create an enum type with some associated data in each variant.
  • If you are just trying to write code that works with many types sharing some common properties, you can use a trait and write code that is generic over that trait.
2 Likes

And if you're trying to convert int bits to a float, use

https://doc.rust-lang.org/std/primitive.f64.html#method.from_bits

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.