How rust works in it's most simple form is that the compiler proofs on the basis of all your type, mutability, and lifetime annotations that there is memory safety. The heap and the stack are tools rust uses in the background and that can help you to be more efficient but not the main thing for people unfamiliar with rust.
Arc a shared pointer. A Rc is the same as an Arc, except that it's not thread safe / atomic to clone & drop. but gives better performance as its less complicated to implement & use on the cpu An Box is a unique ptr
I mean, C++ can also work like this. A lot of the newer standard library does. The only time you are guaranteed to put something on the heap in C++ is when you use the new key, but a lot of stuff, like std::string is also generally a stack item with a small string optimization. Also, you can have both c++ std::string and Rusts String object on the heap with the pointer being a pointer into somewhere else on the heap. Of you have a boxed object, say. Or an arc, even! With c++, you just do new std::string and you get a pointer instead of the string object.
@@NihadBadalov heap allocation is more expensive, so you'll want to use it only when you need to. In particular, it's because dynamic memory allocation is a more complicated algorithm, which in some cases requieres OS calls, which are slow.
@@NihadBadalov It only matters in compute constraints. This video doesn't really explain what you need to know about rust. The important part is that the types, enums, and lifetimes create a system that allows the compiler to proof the memory safety of your program.
Considering just how much rust does in that compilation process that other languages need to do at runtime, it's understandable. Plus, a tiny bit of optimizations (lld, codegen-cranelift, disabling debug symbols for dependencies) can reduce the time by a ton.
@@tiagomello How can I be? I've programmed in nearly a dozen languages as a software engineer for over 20 years without being conscious of any of this. If you have to know this sort of thing for general purpose programming to effectively use Rust then Rust is no good.
Memorised and added to CV
But did you clone it when you moved it from your memory to your CV? Did you copy the underlying data, or do you have two pointers now?
The counter in Arc is also on the heap, so every instance manipulates the same counter
How rust works in it's most simple form is that the compiler proofs on the basis of all your type, mutability, and lifetime annotations that there is memory safety. The heap and the stack are tools rust uses in the background and that can help you to be more efficient but not the main thing for people unfamiliar with rust.
Arc a shared pointer.
A Rc is the same as an Arc, except that it's not thread safe / atomic to clone & drop. but gives better performance as its less complicated to implement & use on the cpu
An Box is a unique ptr
I mean, C++ can also work like this. A lot of the newer standard library does. The only time you are guaranteed to put something on the heap in C++ is when you use the new key, but a lot of stuff, like std::string is also generally a stack item with a small string optimization. Also, you can have both c++ std::string and Rusts String object on the heap with the pointer being a pointer into somewhere else on the heap. Of you have a boxed object, say. Or an arc, even! With c++, you just do new std::string and you get a pointer instead of the string object.
Except C++ is not memory safe and anyone doing anything low level should just use Rust henceforth.
@@philipehusani yes, but this isn't about safety, this is about memory layouts
@@nordgaren2358 Zig?
I feel that lifetimes are an abstraction leakage from the compiler to the user...
what’s the best resource to learn rust
The Rust official book
@@NihadBadalov Honestly, skimming through the official book and immediately starting to code in Rust is the best way
thanks guys
Look up rustlings also alongside the book
Ready to hear some hate.
Uhuh, you don't say?
(Like I respond to my grandma)
also, why do you guys care if it’s in the stack or the heap
Variables which size is not statically known (like dynamically sized arrays) have to be allocated on the heap.
@@aviadisto thank you, but why does that matter?
I mean, why is it important to know that stuff?
@@NihadBadalov heap allocation is more expensive, so you'll want to use it only when you need to. In particular, it's because dynamic memory allocation is a more complicated algorithm, which in some cases requieres OS calls, which are slow.
@@NihadBadalov It only matters in compute constraints. This video doesn't really explain what you need to know about rust. The important part is that the types, enums, and lifetimes create a system that allows the compiler to proof the memory safety of your program.
Rust takes forever to compile
Considering just how much rust does in that compilation process that other languages need to do at runtime, it's understandable.
Plus, a tiny bit of optimizations (lld, codegen-cranelift, disabling debug symbols for dependencies) can reduce the time by a ton.
You clearly haven’t experienced a sufficiently complex C++ codebase.
Honestly, it's not that long. You have to wait like 2 sec when after a successful build
Wrong
Shouldn't have to know any of this to effectively use a programming language.
You’re wrong
@@tiagomello How can I be? I've programmed in nearly a dozen languages as a software engineer for over 20 years without being conscious of any of this. If you have to know this sort of thing for general purpose programming to effectively use Rust then Rust is no good.
@@tiagomello No I'm not. You have your head way up your ass.