←back to thread

177 points signa11 | 4 comments | | HN request time: 0s | source
Show context
kstrauser ◴[] No.42160831[source]
Rust was a pain in the ass until I stopped trying to write C code in it and started writing idiomatic Rust. I don’t know the author of this blog, but he mentions extensive C++ experience which makes me wonder if he’s trying to write C++ in Rust.

Maybe not! Maybe it’s truly just Rust being stubborn and difficult. However, it’s such an easy trap to fall into that I’ve gotta think it’s at least possible.

replies(3): >>42160844 #>>42161027 #>>42161181 #
maguirre ◴[] No.42160844[source]
Are there examples one can learn from about idiomatic rust? I would appreciate either books or projects to learn from.
replies(3): >>42160890 #>>42160891 #>>42161001 #
1. galangalalgol ◴[] No.42160891[source]
Rust, like ocaml, is best when used purely functionally until you run into something that isn't performant unless its imperative. But unlike ocaml or haskell there is a safe imperative middle ground before going all the way to unsafe. People who write modern C++ with value semantics etc. seem to have a lot less trouble than people coming from Java.
replies(3): >>42161004 #>>42161039 #>>42165146 #
2. lowbloodsugar ◴[] No.42161004[source]
I mean, I don’t write it that way, but if it works for you. I wouldn’t say you have to write it that way so I wouldn’t want to put anyone off.

Thinking about your answer a bit more, one of the paradigms of Rust is “there shall be many immutable references or just one mutable reference” and so I can see that functional programming would naturally lead to that. But it’s a paradigm that works with the underlying principles rather than the true nature of the language, IMHO.

I do it by thinking about different domains of object graphs, and how data moves between them, for example.

3. nh2 ◴[] No.42161039[source]
It's difficult to really use Rust purely functionally given that it removed pure functions from its type system, and that has a limited stack size.
4. IshKebab ◴[] No.42165146[source]
Most Rust code is not purely functional in my experience. It's quite similar in style to C++ except:

1. No class hierarchies and inheritance.

2. The borrow checker forces a tree structured ownership style. You don't get spaghetti ownership. This is generally great because that coding style leads to fewer bugs. But sometimes it is annoying and you have to use indices rather than pointers as references.