64
submitted 2 days ago* (last edited 2 days ago) by little_ferris@programming.dev to c/rust@programming.dev

If we were to create a Rust version of this page for Haskell, what cool programming techniques would you add to it?

you are viewing a single comment's thread
view the rest of the comments
[-] barsoap@lemm.ee 1 points 3 hours ago* (last edited 3 hours ago)

Rust has impl Add<&str> for String and impl AddAssign<&str> for String. Both append as expected.

I wouldn't go so far and say "as expected": "Addition" implies that we're dealing with a ring, that there's also multiplication, and that really doesn't make sense for strings (unless you indeed consider them numbers). It's why Haskell's Monoid typeclass comes with the function mappend, not madd.

In Rust's defence though std::ops traits aren't meant to carry any semantics they're about syntax: It's called Add because it's +, not because it means something. I do think it would've been worth it to introduce ++ for general appending (also vectors, sets, maps, etc), though, to keep things clean. Also ++= for the mutating case.

[-] BB_C@programming.dev 1 points 2 hours ago

I do think it would’ve been worth it to introduce ++ for general appending.

I already mentioned (val..).next() which is both safe* and explicit about it being a generic stepping operation instead of possibly being sugar for {x = x + 1; x}.

Also, calling it "appending" is weird for us folks coming from languages like C 😉

* you don't have to worry about what i32::MAX++ would/should return.

[-] barsoap@lemm.ee 1 points 1 hour ago* (last edited 1 hour ago)

Pre- and post-increment are only really useful when you're doing C-style looping and there's a good reason we don't do that in Rust.

I actually honestly can't recall ever making an off by one error in Rust, I'm sure when implementing specific data structures or when doing pointer manipulation it's still a possibility but you can write a gazillion lines of code without ever running risk of that particular annoyance. Also while C folks may have an argument regarding operator semantics, C++ folks don't they're doing unspeakable things to <<.

Also, FWIW Haskell uses ++ to append lists and therefore also strings. It's not like it's an odd-ball usage of the symbols, that'd be .. which I vaguely remember some language using. Would cause a whole new class of confusion regarding 'a'..'z' vs. "a".."z". Not to mention that "aa".."zz" actually makes sense as a range all that's missing is &str: Step. Probably not a good idea to have built-in because do we mean printable ASCII? Whole unicode range? Just the alphabet? Not an issue when you're doing it to single chars but strings get ambiguous fast. Does Rust even guarantee stuff about Char ordering C certainly doesn't really do that, short of I think 0..9 being contiguous.

this post was submitted on 16 Oct 2024
64 points (97.1% liked)

Rust

5876 readers
117 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 1 year ago
MODERATORS