News for hackers. Stuff that matters.
Re:Zero to Production in Rust by Cyph0n on Saturday June 05, 2021 @12:45PM (#27406544)
Can you expand on “needs a C++ or Scala or Haskell background”? C++ would probably help you appreciate what Rust brings to the table, but I don’t see how any of them fit in when learning to use Rust in practice.
Re:Zero to Production in Rust by steveklabnik on Saturday June 05, 2021 @01:08PM (#27406758)
One way of looking at Rust is that it is kinda at the intersection of three different language families: {Ruby,Python,JavaScript} x {C,C++} x {OCaml, Haskell, Scala}

“Needs” is a bit strong but “may find some things familiar and therefore easier” is a common refrain.

Re:Zero to Production in Rust by Cyph0n on Saturday June 05, 2021 @01:15PM (#27406817)
That makes more sense. I'd probably phrase it as: "if you are familiar with a lower-level language and a functional language, you'll likely have an easier time learning Rust".
Re:Zero to Production in Rust by steveklabnik on Saturday June 05, 2021 @01:32PM (#27406975)
Yup, I’d agree with that.
Re:Zero to Production in Rust by ibraheemdev on Saturday June 05, 2021 @01:10PM (#27406774)
Not the OP, but I agree. Also, coming to Rust from a higher level language is very hard. Beginners who do so cannot understand why something like this:

    fn foo() -> &str {
        &String::from("foo")
    }
Does not work. Whereas coming from C or C++, you will appreciate the safety guarantees the borrow checker is giving you.

Learning Rust coming from an OO background is also hard, as you have to adapt to the more functional approach of the Rust type system.

Re:Zero to Production in Rust by nicoburns on Saturday June 05, 2021 @03:04PM (#27407693)
There's definitely content to be learnt. But I'm not sure I'd say it's very hard. I came from JavaScript, and I found that the Rust Book was very good at explaining these kind of issues, so even though they were new to me they didn't cause too many problems.
Re:Zero to Production in Rust by Cyph0n on Saturday June 05, 2021 @01:19PM (#27406853)
That makes more sense - it's definitely true that knowing a lower-level language and a functional language gives you a leg up when learning Rust.

As for your example, even C and C++ devs would squint a bit at why that code doesn't compile: you're allocating a string on the heap, so why can't you return a pointer to it? Also, I think str vs. String is usually confusing at first, regardless of your background.

Re:Zero to Production in Rust by tialaramex on Saturday June 05, 2021 @02:35PM (#27407495)
I think analogous C++ code is a bug, the text might be a heap allocation (SSO may stash it inside the object) but surely the string object is local and lives on the stack. When the function returns that local object no longer exists and the reference to it is now dangling ?

I would say that the relationship between str and String was what most surprised me. I at first assumed the underlying primitive would be String since str is what you get when you borrow from a String, but it isn't. str is a built-in, whereas String is just a type defined in the (optional, this is a systems language) standard library.

If your environment can't afford memory allocation, you can't have Strings but you can use str just fine, that's a built-in. Since you lack allocators obviously you won't be going around minting new strings but str is perfectly fine for stuff like substring operations or comparisons.

Re:Zero to Production in Rust by ibraheemdev on Saturday June 05, 2021 @01:25PM (#27406909)
> Also, I think str vs. String is usually confusing at first, regardless of your background.

Yes, of course. Rust has some new concepts that will be confusing at first, regardless of your background. You still have to understand Rust's destructors to understand that example. I didn't mean to say that it is impossible for Rubyists to learn Rust, or that C++ devs would understand Rust immediately.

Perhaps a better example:

    fn foo() -> &usize {
        let x = 50;
        &x
    }
Re:Zero to Production in Rust by phibz on Saturday June 05, 2021 @03:24PM (#27407827)
From my experience I found the ideas of move semantics from modern C++ directly relatable to ownership and move vs borrow. I also found the FP ideas from Scala, pattern matching, combinators, result and option monads directly useful in rust.
Re:Zero to Production in Rust by galangalalgol on Saturday June 05, 2021 @01:12PM (#27406793)
I would have said it was mor like ocaml.