00:48 actually string literals like the one you have on your example are stored on the static memory along with the program binary in the code segment, to have a string in the heap you need to use the String type.
I was thinking the same thing but I believe there's more nuance to it. A String type is always stored on the heap but an &str type is more complex and can be mutable in some cases. It's often allocated to the stack but sometimes can be a heap reference and sometimes it can just simply be embedded into the code when compiled.
Questa serie di video è davvero ben fatta, complimenti. Spero che aggiungerai altri video con argomenti più complessi di questo linguaggio perchè li spiegi davvero molto bene. Grazie per aver disattivato il plugin per rust di visual code perchè in questo modo il codice che mostri è molto più chiaro e guardando il video non si è distratti da tutti i messaggi invasivi che crea il plugin ogni volta che muovi il mouse sopra una parola. Scelta molto intelligente.
does Rust do any small string optimization - like the C++ std::string implementations usually do? (Will store the string entirely in stack memory if is small enough to fit)
Good question! Rust does not use SSO by design, see this thread if you are interested: news.ycombinator.com/item?id=18372332 Rust uses a slightly different approach compared to C++ and string references are heavily used compared to string objects (thanks to the borrow checker making sure those references are still valid), so missing SSO doesn't usually impact performance much. That said, there are packages to bring SSO if you really need it, such as SmallVec: docs.rs/smallvec/1.6.1/smallvec/
The stack are typically for values that are cheap to create or don't change over time at compile time (take a predictable amount of memory), it also stores the pointers to pieces of data that the length can change and is not known at compile time. Values stored in the stack can include for example numbers, boolean values, string literals and more. The heap stores data that can change, for example a Vec, Hashmap and other structures can have other values pushed into them. This is an oversimplification but I hope it help as a starting point to understanding it better. 🙏
String literals can be saved onto the binary of the program. They are usually stored in the .rodata section of the executable. You need to work with an assembly language in order to understand it.
If you are interested in these topics, you can also follow me on Twitter :) twitter.com/terzi_federico
00:48 actually string literals like the one you have on your example are stored on the static memory along with the program binary in the code segment, to have a string in the heap you need to use the String type.
You are correct. The video provides incorrect information at that timestamp
I was thinking the same thing but I believe there's more nuance to it. A String type is always stored on the heap but an &str type is more complex and can be mutable in some cases. It's often allocated to the stack but sometimes can be a heap reference and sometimes it can just simply be embedded into the code when compiled.
The example shows a &str not a String @@csalmeida
That's correct, @@rochacbruno, not saying a String was used in the example, just trying to expand on what @codeshowbr said. :pray:
Crystal clear explanations! Thank you!!!
Thanks! Glad you liked it :)
Questa serie di video è davvero ben fatta, complimenti. Spero che aggiungerai altri video con argomenti più complessi di questo linguaggio perchè li spiegi davvero molto bene. Grazie per aver disattivato il plugin per rust di visual code perchè in questo modo il codice che mostri è molto più chiaro e guardando il video non si è distratti da tutti i messaggi invasivi che crea il plugin ogni volta che muovi il mouse sopra una parola. Scelta molto intelligente.
Grazie! Felice ti sia piaciuta :)
3:58 please explain why i isnt known at compile time 😮
Really helpful 👍 thanks for the explanation
Thanks!
does Rust do any small string optimization - like the C++ std::string implementations usually do? (Will store the string entirely in stack memory if is small enough to fit)
Good question! Rust does not use SSO by design, see this thread if you are interested: news.ycombinator.com/item?id=18372332
Rust uses a slightly different approach compared to C++ and string references are heavily used compared to string objects (thanks to the borrow checker making sure those references are still valid), so missing SSO doesn't usually impact performance much. That said, there are packages to bring SSO if you really need it, such as SmallVec: docs.rs/smallvec/1.6.1/smallvec/
I want to understand that when should you use heap vs stack in Rust? This is similar case in array vs vector. Can you please help...
The stack are typically for values that are cheap to create or don't change over time at compile time (take a predictable amount of memory), it also stores the pointers to pieces of data that the length can change and is not known at compile time. Values stored in the stack can include for example numbers, boolean values, string literals and more. The heap stores data that can change, for example a Vec, Hashmap and other structures can have other values pushed into them. This is an oversimplification but I hope it help as a starting point to understanding it better. 🙏
@@csalmeida thanks for the explanation. Really appreciate it 👍🏻
very helpful video thank you!
Thanks :)
Thanks Federico!
Excelent explanation
Thanks!
Sorry to bother, but what is your sound set up (mic, interface, etc)? Sounds really good. Great video by the way!
I've used a Blue Yeti mic :)
thank you
Good explanation
nice 🔥
What do you mean when you say the string is stored on the binary itself?
String literals can be saved onto the binary of the program. They are usually stored in the .rodata section of the executable. You need to work with an assembly language in order to understand it.
Are you gonna start posting regularly again?
Yes, definitely! But it will likely take a while as I'm going through a busy period :)
@@FedericoTerzi Ok great
Thank you!
Great video. Thanks
Glad you liked it! Thanks :)
Are you by any chance related to Neymar
Never used var in my python programs! Guess ya learn something new everyday hey?
/s
😂
you are handsome