If I return a Vec buffer and a pointer to its internal data, is the pointer valid?

I’m writing some C FFI bindings, and I came up with a situation which I’m unsure whether it works or not. In its simplest form, it would be: unsafe fn foo() -> (*const u8, Vec<u8>) { let buf = vec![0, 1, 2]; (buf.as_ptr(), buf) } Now using it: fn main() { let (ptr, _buf) =… Read More If I return a Vec buffer and a pointer to its internal data, is the pointer valid?

How can I transmute a `MaybeUninit<T>` to a `T`?

I’d like to transmute a MaybeUninit<T> to a T. use std::mem::transmute; use std::mem::MaybeUninit; fn make_init<T: Sized>(mt: MaybeUninit<T>) -> T { unsafe { transmute(mt) } } fn main() { make_init(MaybeUninit::new(1)); } However this program gives me: error[E0512]: cannot transmute between types of different sizes, or dependently-sized types What’s the right way to do this? >Solution :… Read More How can I transmute a `MaybeUninit<T>` to a `T`?

How to convert lifetime of boxed reference without allocating a new box?

Context Playground, This works: fn get_owned_box_working<‘a>(b: Box<&’a i32>) -> Box<&’static i32> { Box::new(&42) } but this doesn’t: fn get_owned_box_broken<‘a>(b: Box<&’a i32>) -> Box<&’static i32> { *b = &42; b } error[E0308]: mismatched types –> src/lib.rs:3:5 | 3 | b | ^ lifetime mismatch | = note: expected struct `Box<&’static i32>` found struct `Box<&’a i32>` note:… Read More How to convert lifetime of boxed reference without allocating a new box?

HTML, Customization Problems in Web Page's Footer Section

The code: (Between footer=""" and """ includes html format code) footer="""<meta name="viewport" content="width=device-width, initial-scale=1"> <style> a:link , a:visited{ color: blue; background-color: transparent; text-decoration: underline; } a:hover, a:active { color: red; background-color: transparent; text-decoration: underline; } .footer { position: static ; left: 0; bottom:0; width:100%; background-color:white ; color: black; text-align: center; } </style> <div class="header"> <p><b>Get… Read More HTML, Customization Problems in Web Page's Footer Section