Do all the things like ++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatarSign Up
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple APILearn More
Hello! 🙋 Been looking into Rust for a while now, and about how it manages memory at compile time (at least from what I understood) is that the language has a feature known as the 'borrow checker'. As you're aware, all programs have to manage the way they use a computer's resources while running. Some programming languages like Go and C# employ a garbage collector that keeps track of the resources and releases them when they're no longer in use. Rust however, takes a different path. Employing it's central feature called 'ownership', the language manages memory through a system of ownership rules that the compiler checks at compile time. Here are some of the rules:
1. Each value in Rust has a variable that's called it's 'owner'.
2. There can only be one owner at a time.
3. When the owner goes out of scope, the value will be dropped.
In your journey of Rust, you might encounter heavy usage of 'borrowing', which you can think of as having (a) reference(s) to data objects (often referred to as 'variable bindings' in the language). During compile time, your Rust code is taken through Rust's borrow checker, which ensures that all the ownership rules have been followed. Insidious, and often hard to trace bugs such as data races, deadlocks and even memory leaks, common in other languages are thus prevented. In fact, Rust code with data races won't even ever compile, forcing you to fix the very bug. As you can see, Rust is a language that can really ensure memory safety of your code, and what's more glorious is that it does this at compile time!
Oh, and by the way, if you have Rust installed, you can issue the command : ( rustup doc) from your command prompt/shell. This will open the official Rust documentation on your browser, and thus you can be able to learn more about the language.
Hope this helps. Best wishes😊
I second @ImperatorKylisa. Rusts memory management basically gives you the safety of java with the speed/optimization of c/c++.
I expect great things from this language in the future.
@ImperatorKylisa oh I really appreciated for your explanation. Is its way memory handling similar with objective-c? I didn't write obj-c but as far as I know it works a way something like that.
@Furyzer00 i'd say it's like c/c++, but the compiler does all the (de)allocating. By giving each object a lifetime you already know at compile time how long it will be valid and when you can delete it.
Think of a
// do something
The compiler will allocate memory for the object at the beginning and free it afterwards.
If that function would call another function and pass the object, the compiler wouldn't insert the code to free the object. It is the other functions responsibility now because that function claimed ownership of the object (you can create functions that dont claim ownership too). The compiler knows that this function gets ownership of whatever is passed to it and will insert the required code to free the object.
There is more to memory-managing in rust, but that should be the most important things. The fact that firefox quantom is made with rust components shows that rust has great potencial.
@ImperatorKylisa no, i only read the official tutorial after a friend of mine, totally fascinated by rust, talked me into it. ^^"
But i really like the way of giving the compiler more information to create safer code. Same with "panic as default error" and non-nullable types. My next project that needs speed will use rust for sure.