T to &'static T

  Рет қаралды 4,029

timClicks

timClicks

Күн бұрын

Пікірлер: 28
@rodrigo-5967
@rodrigo-5967 Жыл бұрын
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 :)
@markmcdonnell
@markmcdonnell 11 ай бұрын
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 😊
@agustindiaz3361
@agustindiaz3361 10 ай бұрын
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
@headlibrarian1996
@headlibrarian1996 5 ай бұрын
Why would you want to do that?
@Rose-ec6he
@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
@lack_of_awareness Жыл бұрын
Please be careful not to leak everything and have ur program eating ram indefinitely
@lack_of_awareness
@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
@francomusolino6846
@francomusolino6846 11 ай бұрын
thanks Tim
@TN19N
@TN19N Жыл бұрын
thank you for your time
@p4luagecehmg810
@p4luagecehmg810 Жыл бұрын
great video. just curious, what's the name of that VS Code theme/color palette and the font? cheers!
@aaran5914
@aaran5914 11 ай бұрын
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
@aaran5914
@aaran5914 11 ай бұрын
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
@earlylate3308 Жыл бұрын
Very interesting video! What are the performance differences between using this approach vs. using the Lazy struct of the once_cell crate?
@dmitriidemenev5258
@dmitriidemenev5258 Жыл бұрын
I was about to ask this very question!
@greisboy425
@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
@goodnewsjohn2482
@goodnewsjohn2482 2 ай бұрын
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
@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
@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
@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
@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
@RobertKing Жыл бұрын
thread::Builder::new() .stack_size(32 * 1024 * 1024) // 32 MB .spawn(||
@vytah
@vytah Жыл бұрын
@@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
@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
@RobertKing Жыл бұрын
Fun trick thanks
Жыл бұрын
Could the OS reuse leaked memory address?
@timClicks
@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.
Traits are weird
3:16
timClicks
Рет қаралды 1,6 М.
Tonic makes gRPC in Rust stupidly simple
19:08
Dreams of Code
Рет қаралды 52 М.
Don't underestimate anyone
00:47
奇軒Tricking
Рет қаралды 27 МЛН
When Cucumbers Meet PVC Pipe The Results Are Wild! 🤭
00:44
Crafty Buddy
Рет қаралды 62 МЛН
One day.. 🙌
00:33
Celine Dept
Рет қаралды 61 МЛН
За кого болели?😂
00:18
МЯТНАЯ ФАНТА
Рет қаралды 3,5 МЛН
Essential Data Structures in Rust (Part 6)
16:20
OptiCode
Рет қаралды 80
You Should Really Know These Traits in Rust
18:36
Oliver Jumpertz
Рет қаралды 16 М.
Rust's iterators are more interesting than they look
22:46
timClicks
Рет қаралды 9 М.
Rust's lifetimes made easy
6:39
timClicks
Рет қаралды 13 М.
Use Arc Instead of Vec
15:21
Logan Smith
Рет қаралды 153 М.
iced GUI | Rust Language
17:00
Learning Rust
Рет қаралды 10 М.
Async Not Required 🦀
11:52
timClicks
Рет қаралды 5 М.
Smarter Programming in Rust: Master 'move' Now!
9:06
Flo Woelki
Рет қаралды 4,9 М.
RUST Enums ARE Better
5:49
ThePrimeagen
Рет қаралды 145 М.
Rust's Most Important Containers 📦 10 Useful Patterns
17:11
Code to the Moon
Рет қаралды 129 М.
Don't underestimate anyone
00:47
奇軒Tricking
Рет қаралды 27 МЛН