[][src]Function fuckit::immortalize

pub fn immortalize<T>(value: T) -> &'static mut T

Given a statically-sized value, allocates new memory for it which will never be freed and moves (or copies) the value to that memory, where it will have an unbounded 'static lifetime, and returns a mutable reference. This leaks memory.

Example

Returning a reference to a locally-created value without manually making it owned by some longer-lived value.

fn inner_function() -> &'static mut Vec<usize> {
    let on_stack = vec![1, 2, 3];
    let on_heap = fuckit::immortalize(on_stack);
    on_heap
}

let v = inner_function();

assert_eq!(*v, vec![1, 2, 3]);