How to make a temporally leaking(get static ref)

use std::mem::ManuallyDrop;

fn main() {
    let scoped_string = "Hello, world!".to_string();

    let manually = ManuallyDrop::new(scoped_string);

    take_static_str(manually.as_ref()); // not allowed

fn take_static_str(s: &'static str) {
    println!("take_static_str: {}", s);

I know I i can just call Box::leak, but that will leak it without any recover mechanism, it would be nice I can take static ref from ManuallyDrop and then when I know manually is not been used anymore, I can manually drop it

for example

if I use this function, I need a static-ish string ref, but I want to use format!, this is resulting as a Box::leak(format!("Installing {}", version.clone().unwrap_or_else(|| "latest".to_string())).into_boxed_str()) for now, but it's clearly that when the progressbar dropped I can safely recover the leak.

You can use Box::from_raw to convert 'static reference back to a Box.

Note that regardless of how you implement that, it's all dangerous. "Temporary 'static" is not a thing that can exist. 'static basically bypasses borrow checking and promises this reference to be valid forever, so mere ability to go back to destructable Box makes it untrue.

1 Like

But that would be "rust is forcing me to leak the memory when I use functions that takes static ref, and I don't have one except leak a scoped box", which IMO is not acceptable a memory safe language would do that, memory leaking is also memory unsafe, isn't it?

Leaking memory is not unsafe.

Leak-them-all is the semantics of almost every garbage collected languages. The program constantly allocate memory and leak them. And the garbage collector in the back stage somehow magically detect memory allocations which became inaccessible and free them, or not.


'static reference is a very special edge case, and Rust programs don't use them generally. If you find yourself making one, you're way off track.

Note that lifetimes apply only to borrowed data, not to self-contained types like String.

Cow<'static, str> type is used to avoid allocations if you use hardcoded strings. It is an enum that either takes a string literal compiled into the executable, or a heap-allocated String. The effective lifetime of the string may be short, but that notion of a lifetime is a different thing from lifetime annotations in Rust that are for borrowed data.

You also find the compiler error saying "reference must be valid for 'static lifetime". That is trying to say "all temporary references are forbidden here, and you must use Arc instead". It typically happens when you try to use self. inside closures that are given to threads.

3 Likes means that the function is free to manipulate passed data as long as it needs - including, e.g., storing it in the thread-local or sending to long-lived thread. If the function really requires the data to be 'static (and isn't just overzealous with its signature, in which case the function itself should be changed) - passing there a fabricated &'static reference isn't good at all.

1 Like

Thank you all for helping me out :smile:

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.