Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make method return String to be &'static str #68

Closed
baoyachi opened this issue Jan 16, 2022 · 2 comments
Closed

make method return String to be &'static str #68

baoyachi opened this issue Jan 16, 2022 · 2 comments
Labels
refercence reference link

Comments

@baoyachi
Copy link
Owner

baoyachi commented Jan 16, 2022

#[repr(C)]
pub union PtrToRef<'a, T: ?Sized> {
    pub ptr: *const T,
    pub reff: &'a T,
}

fn main() {
    let hello = b"Hello union Rust";
    let key = unsafe {
        let string: &'static str = {
            PtrToRef {
                ptr: hello as *const [u8] as *const str,
            }.reff
        };
        string
    };
    println!("{}", key);
}
@baoyachi
Copy link
Owner Author

baoyachi commented Feb 22, 2022

@baoyachi
Copy link
Owner Author

use const_format crate solve

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refercence reference link
Projects
None yet
Development

No branches or pull requests

1 participant