
-----------------------------------
wtd
Fri Nov 12, 2021 7:29 pm

Rust
-----------------------------------
So, any compsci.ca members programming in Rust these days?

-----------------------------------
SNIPERDUDE
Sat Nov 13, 2021 7:44 am

RE:Rust
-----------------------------------
Haven't looked too much into Rust, what are it's strengths?

These days I'm pretty much just using Godot (gdScript, based on python)

-----------------------------------
scholarlytutor
Sat Nov 13, 2021 7:46 am

RE:Rust
-----------------------------------
I've been hearing more and more about Rust and it looks interesting. But since I'm more interested in old software, C and C++ are more relevant for me. Do you program in Rust?

-----------------------------------
wtd
Sat Nov 13, 2021 11:19 am

RE:Rust
-----------------------------------
I have dabbled, as is my wont. So far the details of lifetimes are kicking my butt.

To me it looks like OCaml because it incorporates a lot of ML-y fucntional programming ideas about type inference, pattern-matching, etc. But without garbage collection, in many ways it's worse.

I guess the best way to think about it would be a modern, better C or C++ replacement, since the lack of garbage collection makes it better suited for low level system programming than a lot of other modern programming languages.

-----------------------------------
wtd
Sat Nov 13, 2021 11:19 am

RE:Rust
-----------------------------------
I have dabbled, as is my wont. So far the details of lifetimes are kicking my butt.

To me it looks like OCaml because it incorporates a lot of ML-y fucntional programming ideas about type inference, pattern-matching, etc. But without garbage collection, in many ways it's worse.

I guess the best way to think about it would be a modern, better C or C++ replacement, since the lack of garbage collection makes it better suited for low level system programming than a lot of other modern programming languages.

-----------------------------------
scholarlytutor
Sun Nov 14, 2021 1:07 pm

RE:Rust
-----------------------------------
Yes, that’s what I’ve heard too, using Rust as an alternative to C/C++. It’s safer to use Rust because I believe it’s easier to avoid problems like memory leaks, but it also has better speed than most higher level languages (though I’m guessing it’s not quite as fast as C).

Apparently there are people in the Linux community who really like it. There’s some talk about writing the Linux kernel in Rust; right now it’s written in C.

-----------------------------------
wtd
Sun Nov 14, 2021 3:42 pm

RE:Rust
-----------------------------------
The compiler is very strict, especially about memory issues. It uses the concept of lifetimes to manage access to memory. It's precisely this that kicks my butt.

-----------------------------------
SNIPERDUDE
Mon Nov 15, 2021 9:44 am

RE:Rust
-----------------------------------
Definitely adds a twist I'm not used to. May play around with it a bit over the weekend

-----------------------------------
wtd
Mon Nov 15, 2021 12:09 pm

RE:Rust
-----------------------------------
Good! Be sure to share. Maybe we can help each other.

-----------------------------------
scholarlytutor
Mon Nov 15, 2021 12:32 pm

Re: Rust
-----------------------------------
That's interesting, so the compiler forces you to solve any memory issues.

Maybe I'll take a look at Rust too, though I doubt I know as much about memory management as you guys do. And I know absolutely nothing about lifetimes.

-----------------------------------
wtd
Mon Nov 15, 2021 1:01 pm

RE:Rust
-----------------------------------
Judging from my difficulties, I don't know anything either! ;)

-----------------------------------
wtd
Mon Nov 15, 2021 2:13 pm

Re: Rust
-----------------------------------
But seriously, consider the following in C.

#include 

int foo() {
    int x = 42;
    return x;
}

int main() {
    int y = foo();

    printf("%d\n", y);
}

No issues because while the lifetime of x is limited to the function foo, it's returned by value. A copy of x is made, x dies, and that copy lives on.

But what about:

#include 

int *foo() {
    int x = 42;
    return &x;
}

int main() {
    int *y = foo();

    printf("%d\n", *y);
}

Uh oh. We're trying to return the address of a value whose lifetime has ended. This invokes undefined behavior. A good compiler will warn about this, but it will still compile. Because the behavior is undefined, it might even work sometimes. But it might not, and that leads to bugs.

So let's look at two samples of Rust code that approximate these C examples.

(I'm still learning) use a Box to create a smart pointer, and dereference it with *.

[code]fn foo() -> Box {
    let x = 42;
    Box::new(x)
} 

fn main() {
    let y = foo();
    println("{}", *y)
}[/code]

Unless otherwise specified, bindings in Rust are constant. So let's mark something as mutable.

[code]fn foo() -> Box {
    let x = 42;
    let mut z = Box::new(x);

    *z += 2;

    z
} 

fn main() {
    let y = foo();
    println("{}", *y)
}[/code]

Now this should print 44.  But bear in mind that y is still constant, so we can't mutate that further without marking it mutable.

-----------------------------------
SNIPERDUDE
Mon Nov 15, 2021 3:01 pm

Re: Rust
-----------------------------------
More to come
Hope so, been quite informative thus far
