let mut x:*const ();
x=&1234;
If i want to chang integer pointer to float it dosent compile..
x = &44.4;
- () type is general type or tuple type?
2.how to assign any type to x.
let mut x:*const ();
x=&1234;
If i want to chang integer pointer to float it dosent compile..
x = &44.4;
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.
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.
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:
()
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.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?
&dyn Display
).enum
type with some associated data in each variant.And if you're trying to convert int bits to a float, use
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.