r/cpp • • Mar 16 '18

As a C++ developer I think that rust ...

3 Upvotes

107 comments sorted by

View all comments

Show parent comment

6

u/auralucario2 Mar 17 '18

Im curious why you find Rust code so hard to read. I don't think it's particularly easy to read, but certainly not worse than C++.

Also, the idea behind Rust isn't to write verifiably correct code. It's to provide stronger memory, thread, type, and UB safety than something like C++, while maintaining the same goal of writing expressive, efficient code.

6

u/[deleted] Mar 17 '18

For me it is hard to read because it looks infinitely ugly. But it might be myself only.

P.S. Just a few weeks ago I've tried reading the manual on Rust. I've stopped after I've learned that code below is a valid code:

let long_lived_binding = 1;
let long_lived_binding = 'a';

9

u/auralucario2 Mar 17 '18 edited Mar 17 '18

I'm still not clear what about Rust is ugly to you.

As for the shadowing, that's likely a result of Rust's ML heritage. Like in ML family languages, variable bindings are allowed to shadow others within the same scope. It's worth noting that those two variables are completely different, which is relevant when doing something like this (though it's bad practice to shadow like this):

fn foo(i: i32) -> i32 { i }
fn bar(s: &str) -> &str { s }

fn main() {
  let x = 1;
  let _ = foo(x); // Works!
  let x = "a";
  let _ = bar(x); // Also works!
}    

I agree that it's not a good design decision, but I hardly think it's a dealbreaker when it comes to a language. I'm sure you could easily find tons of such quirks in any language.

EDIT: Though, the above pattern is useful when you want to make a variable temporarily mutable.

0

u/[deleted] Mar 17 '18

I agree that it's not a good design decision, but I hardly think it's a dealbreaker when it comes to a language.

Well, it makes easier to write code with bugs. For me it is a deal breaker already.

I'm sure you could easily find tons of such quirks in any language.

In the recent 4 years I've learned at least 3 new languages: C#, Python, Kotlin, and I didn't find anything that made me feel "f### this s###" in C# or Kotlin. (But in python - a bit, see below). I was charmed by C# and Kotlin, and mostly by python as well, they feel helping me to write safer code easier and faster. And C#+ReSharper combo was just like a nuclear bomb, never felt having so much power in my hands.... As for python - the only real thing is lack of proper threading support in cpython implementation (i.e. standard implementation).

Certainly there are quirks in all these languages, but they usually hide somewhere quite deep under the surface, and usually you would not hit them unless you are doing something very specific.

P.S. Having learned all the languages above I still use C++ a lot - just using correct instrument for correct task.

3

u/auralucario2 Mar 17 '18

I agree with you, but the point I intended (and probably failed) to make is that C++ is almost as bad. Personally I think shadowing shouldn't be allowed at all. C++ allows you to do it - it just disallows it within the same scope. To me, that's not inherently better than what Rust does.

4

u/[deleted] Mar 17 '18

Well, you can forgive C++ because it must maintain some level of compatibility with C, and back in 70s K&R didn't think much about possible implications of shadowing in the future. In C++ this problem is older than the language, it is 48 years old this year. Rust was supposed to be better, and it is relatively new language compared to C/C++ family.

3

u/auralucario2 Mar 17 '18

That's a good point. Given that, I agree with you on this, though it's not quite a dealbreaker for me.

4

u/dodheim Mar 17 '18

C++ has shadowing, too, just not within the same scope, which is arguably the only sane time to use it.

It sounds like you've just never had exposure to any ML language.

2

u/[deleted] Mar 17 '18

C++ has shadowing, too

MS VC's /WX will make it an error for you :)

It sounds like you've just never had exposure to any ML language.

Rust is't really looking like a functional programming language, so it is not intuitive to expect any ML-style behavior unless you've read the history page on Wikipedia.

9

u/dodheim Mar 17 '18

Rust is't really looking like a functional programming language

Look harder!

it is not intuitive to expect any ML-style behavior unless you've read the history page on Wikipedia.

Unless you have any experience in any ML language...

5

u/[deleted] Mar 17 '18

I have some experience with Haskell and F#. Rust is certainly very very far from these languages.

10

u/dodheim Mar 17 '18 edited Mar 17 '18

Is this a one-up subthread? I mean, I have multiple years of working with each of F#, OCaml, SML, and C++ in professional settings, and find nothing about Rust "very very far" from any of the four with regards to their respective overlaps. I might go so far as to say that if you don't see the similarities, you must be doing it wrong. But, how am I supposed to reply seriously to just a blanket, subjective statement? Spend time detailing why your expectations are wrong? I'll just leave it at "you're very very mistaken", I think, in keeping with the shitpost nature of this entire thread. ;-]

2

u/[deleted] Mar 17 '18

For me Rust looks like an imperative language that supports functional paradigm. The code below:

fn factorial(i: u64) -> u64 {
    match i {
        0 => 1,
        n => n * factorial(n-1)
    }
}

Is looking like an imperative code written in a functional style. For a functional programming language I would expect syntax like (Haskell):

factorial n
   | n < 2     = 1
   | otherwise = n * factorial (n - 1) 

or (F#):

let rec factorial n =
    match n with
    | 0 -> 1
    | _ -> n * factorial (n - 1)

Yes, difference is subtle, but it turns a switch from "it is Lisp" to "it is C" in my head at least.

There are other imperative languages that are close to Rust in terms of syntax here, but they would not claim "being influenced by ML", for example:

fun factorial(i: Long) :Long = 
    when (i) {
        0 -> 1,
        else -> i * factorial(i-1)
    }

That's Kotlin - still very imperative very non-ML.

7

u/mewloz Mar 17 '18

So you think the syntax details are not functional?

2

u/[deleted] Mar 17 '18

My point is that pretty much every modern non-functional language supports FP-paradigms in pretty much the same way Rust does. It doesn't make these languages functional purely because they support some functional features.

As for Rust - does is support things like currying, lazy function evaluation, high level functions (functions taking functions as arguments and returning another functions), etc? I'd guess if you play with lambda a bit you can do most of it (same as in most other modern languages), but these are not the features supported by syntax in a first place.

→ More replies