6

I was just looking new and lesser known programming languages. Then I came to Rust.
"Rust manages memory at compile time"
What?๐Ÿ˜ต How is that even possible?

Comments
  • 7
    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.
  • 7
    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๐Ÿ˜Š
  • 1
    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.
  • 0
    @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.
  • 1
    Indeed, @YouAreAPIRate. The future of Rust does indeed seem very promising, especially in places like AAA game development currently dominated by languages such as C++. By the way, @YouAreAPIRate, might you have any information on Rustation's recent developments?
  • 1
    @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
    int myfunc(){
    A object;
    // do something
    return 0;
    }

    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.
  • 1
    Neither have I worked with Objective C before, @Furyzer00, but I believe you're right. Something to do with both reference counting and Atomic reference counting, which is supposed to be safe to use across multiple threads (correct me if I'm wrong).
  • 0
    @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.
  • 1
    I totally agree with you, @YouAreAPIRate. Considering Rust is even being used in the machine learning system 'Leaf', yours would be a brilliant choice. Best wishes in all your upcoming Rust projects, @YouAreAPIRate
Add Comment