How to cast it into its original type?

//assume that this function takes any type of variable casts to *const()..
//but inside function i want to cast back it into its original type
//but assume we don't know what is func argument real type right?
//then how to cast it into its original type?

fn ok(z: const ()) {
let x = unsafe {&
(z as *const() as *const Mystruct)};
println!("{:?}", x);
struct Mystruct {}

struct Istruct{}

fn main() {
let x = Mystruct {};
let q = Istruct{};
ok(&x as *const Mystruct as *const ());
ok(&q as *const Istruct as *const ());


Based on your recent posts, it seems you're trying to learn Rust by heavily utilizing the raw pointer types. It's like learning C by heavily utilizing the goto statements and entirely avoid other more idiomatic control flow constructs like if, while, for and function call/return. To write idiomatic Rust you should avoid raw pointers as much as possible, in the same sense you should avoid goto statements as much as possible to write idiomatic C.

If you want to learn this new language but have no idea where else to start, we have a list of official learning materials for beginners. After the course you would be able to write practical applications in Rust. If you feel stuck during the course please feel free to ask it here!


There is no way to check what the original type of an *const () is.


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.