My current code:
use std::alloc::{alloc, dealloc, Layout};
unsafe {
let n = 0x10000; // I get this value from a function
let layout = Layout::new::<[u8; n]>();// n is non-constant value issue E0435 E0425
let mut ptr = alloc(layout);
*(ptr as *mut u8) = 42;
}
What if I want to allocate 0x10000 of heap? I have read Layout documentation but I miss the example about allocating more than one u8. (Layout::new::<u8> works fine)
Is it the best way to allocate memory (unsafely) in Rust? Also what would be the best safe way to do it ?
Alternatively I have been using (which works very well):
let n = 0x10000;
let mut v<u8>; = Vec::with_capacity(n);
unsafe { v.set_len(n); }