Thanks a lot! I just couldn't grasp the concept of static and Box overall, and you made it so clear. I really appreciate it :)
@markmcdonnell11 ай бұрын
This was a fantastic video. I've been aware of this channel/author for a while but I've been working in Go for so long I just dropped out on Rust ror a bit but am back and saving all these videos to my watch later list 😊
@agustindiaz336110 ай бұрын
Nice! Another thing that rust does to simplify this is that you can create references to values (not variables) and they'll have the static lifetime too, like for example &5
@headlibrarian19965 ай бұрын
Why would you want to do that?
@Rose-ec6he Жыл бұрын
What a clever observation. I've used boxes readily and used leak before but I never put 2 and 2 together and realised it returns a mutable static reference. This opens up a lot of doors - now I can have easily mutable statics that are locally scoped and have arbitrary size!
@lack_of_awareness Жыл бұрын
Please be careful not to leak everything and have ur program eating ram indefinitely
@lack_of_awareness Жыл бұрын
Might want to take a look at thread local globals instead , if u want storage for static things that are only required per thread
@francomusolino684611 ай бұрын
thanks Tim
@TN19N Жыл бұрын
thank you for your time
@p4luagecehmg810 Жыл бұрын
great video. just curious, what's the name of that VS Code theme/color palette and the font? cheers!
@aaran591411 ай бұрын
are there some usecases where its useful to do the pattern of leaking dynamic local values in functions and passing refrence to avoid allocating and when the end fn is done with it , it de-allocates it by doing box::from_raw() and drops it
@aaran591411 ай бұрын
or this will be unsound ? cause you would be converting a refrence to *mut t and if it was &'static it was referred else where and dropping it would be potentially unsound behaviour ?
@earlylate3308 Жыл бұрын
Very interesting video! What are the performance differences between using this approach vs. using the Lazy struct of the once_cell crate?
@dmitriidemenev5258 Жыл бұрын
I was about to ask this very question!
@greisboy425 Жыл бұрын
To create static lifetime you can use: - Box::leak(Box::new(SomeStruct)) - will allocate SomeStruct on the heap - Use combination of raw pointer + std::mem::forget(). let ss = SomeStruct; let rp_const = &ss as *const SomeStruct; let static_ref = unsafe { &*rp_const }; // this is how do you fight and defeat borrow checker. :P // ss will be drop at the end of scrope and // static_ref will be pointing to dealocated memory // but we can prevent the drop by using mem::forget() std::mem::forget(ss); static_ref
@goodnewsjohn24822 ай бұрын
I was looking for this comment. In my head the only way to leak is to use a raw-pointer and stop rust from dropping the raw pointer, that way one can always deference such pointer using an unsafe abstraction anywhere in your code
@RobertKing Жыл бұрын
I'd be interested in how to allocate large vector. E.g. 1 billion integers. If the stack runs out of memory, can you box it into the heap?
@greisboy425 Жыл бұрын
What do you mean by saying 1 billion Vec on the stack...?. Vec allocate data on heap not on the stack, you dont need to Box a Vec to force it store data on the heap, because it already on the heap.
@vytah Жыл бұрын
Vector elements are always on the heap. What's on the stack is just one pointer and two integers. And if you were asking about fixed-size arrays (which may reside on stack), then of course you can box them, or turn into vectors, or hide behind an Rc or Arc etc. and move them to the heap that way.
@RobertKing Жыл бұрын
@@vytah Ah you read my mind. I did mean fixed size. Can fixed size be more performant, but more chance of running out of stack memory? Does moving to the heap cost a memcopy? How expensive is that? What's the most performant way to have a list of numbers for 1million,1billion, etc.
@@RobertKing Fixed-size arrays on the stack are the fastest, as you don't have to allocate anything on the heap. The second fastest would be having a Box, then Box, then Vec: all of them use the heap in the same way, but their stack sizes are different: 1, 2, and 3 machine words respectively.
@ScienceMinisterZero Жыл бұрын
That was great, really enjoyed the journey of Box::leak. You could make lots of videos exploring Rust type conversions. Yesterday I learned about how to use Cursor to satisfy apis designed for files.
@RobertKing Жыл бұрын
Fun trick thanks
Жыл бұрын
Could the OS reuse leaked memory address?
@timClicks Жыл бұрын
No. The operating system still considers the memory to be active and won't provide it for other processes. That's why memory leaks can be dangerous. They can cause memory exhaustion.