5 ms·
Rust is hard to write but I'll grant that once written it is relatively easy to understand. That's kinda the opposite of C++ where it's easy to write and hard t
by CalChris 10y ago
Rust is hard to write but I'll grant that once written it is relatively easy to understand. That's kinda the opposite of C++ where it's easy to write and hard to understand (although it's easy to think you understand).
I appreciate the safety+performance value proposition in Rust. More cathedral, less bazaar.
- iopq 10y agoNot THAT easy fn apply<A, B, C, F, G>(mut f: F, a: A) -> impl FnMut(&B) -> C // must still be `for<'r> impl FnMut(&'r B) -> C`, because that’s what filter requires where F: FnMut(B) -> G, // must not be `for<'r> FnMut(&'r B) -> G`, because regular functions do not implement it G: FnMut(A) -> C, B: Copy, // for dereferencing A: Clone { move |b| f(*b)(a.clone()) // this must do any bridging necessary to satisfy the requirements } this function signature must be EXACTLY like what I wrote or it won't work
- CalChris 10y agoNo, not that easy especially when you get to the functional programming stuff. But I find FP hard to understand since I don't use it much. > this function signature must be EXACTLY like what I wrote or it won't work This can be seen as a feature in the sense that you can't program by luck: well, this incantation seems about right and the compiler nods its head that it understands and that means something, so it must be right.
- iopq 10y agoJoke's on you: it took me a month of asking around why my program won't compile to figure out I really CAN'T do it without dereferencing inside the function. This is because of higher ranked lifetime bounds... there's no way to express the correct lifetime bound in this case (in Rust, maybe in Haskell it's possible), so you just have to deref first.
- CalChris 10y agoYou probably have a simpler, clearer way of solving that problem than by torturing the language.
- iopq 10y agoI do, but the problem is FizzBuzz. What's the point of solving FizzBuzz in a SIMPLE way? The whole point of FizzBuzz is to torture the language.
- lisivka 10y agofn main() { for i in 1..101 { if i % 15 == 0 { println!("FizzBuzz"); } else if i % 3 == 0 { println!("Fizz"); } else if i % 5 == 0 { println!("Buzz"); } else { println!("{}", i); } } }
- flukus 10y agoI don't know why people over complicate things. I was searching for other solutions and found this abomination: https://bitbucket.org/iopq/fizzbuzz-in-rust/src/bf4968973d73137f0dfd07205d599bed30a788fa/src/lib.rs?at=master&fileviewer=file-view-default https://bitbucket.org/iopq/fizzbuzz-in-rust/src/bf4968973d73... I hope their production code isn't like this.
- iopq 10y agoI wrote this as an exercise. This is specifically written to be point-free code without any if or match expressions. .unwrap_or_else(|| i.to_string().into()) this is the only one that was too ugly to make point-free
- Ygg2 10y agoWut? It's about as representative of Rust as writing FizzBuzz using Monads. In Java. If you want production code look at Rust and Servo.
- lisivka 10y agoI don't know Rust, but code looks readable. Function apply receives two arguments and returns function of one argument (address of b) which returns value of type C. I assume function apply() creates closure, so a.clone() is necessary to create fresh copy of a every time when closure is called. b is passed by reference, so code is generic. Not bad.
- gpderetta 10y agoEquivalent [1] C++14 for comparison: template<class F, class A> auto apply(F f, A a) { return [f=std::move(f), a=std::move(a)](auto&& b) { return f(std::forward<dectlype(b)>(b))(a); } } The forward<decltype> is an abomination and is really in need of a language based solution, but otherwise is fairly straightforward. [1] No type checking of the definition of course, only of instantiations. On the plus side, the returned function is polymorphic and can be called for all Bs that f can be called.
- eximius 10y agoNot sure what you're talking about: https://is.gd/Xnu0YJ https://is.gd/Xnu0YJ
- iopq 10y agoit doesn't work in my program: https://bitbucket.org/iopq/fizzbuzz-in-rust/src/bf4968973d73137f0dfd07205d599bed30a788fa/src/lib.rs?at=master&fileviewer=file-view-default https://bitbucket.org/iopq/fizzbuzz-in-rust/src/bf4968973d73... it gives an error when I use a generic `second` method from tool.rs
- ytugiuyghvk 10y agoWhy ask that B: Copy instead of B: Clone?
- iopq 10y agoTo deref it
- ytugiuyghvk 10y agob.clone(), of course, derefs b as well.
- hackuser 10y agoIs Rust harder to write than safe, readable C++ code? EDIT: Reworded it to be more specific
- CalChris 10y agoI don't know that good C++ has been standardized or is enforceable by the compiler. But Rust is hard to write and that's doubly especially the case coming from an anything goes language.
- Ygg2 10y agoHeavily disagree. Usually, compiler tells you how to fix your errors. You still need solid grasp of language.
- CalChris 10y agoThe borrow checker can be hard to follow occasionally. Your solid grasp of the language may be different or the same as my hard to write. But neither would be confused with utterly ambiguous C. The counterpart to borrowing in C would be it's antithesis in Rust, aliasing. That said I'm on my learning curve and my opinion could change.
- Ygg2 10y agoWhen was the last time you used Rust? I ask this, because newer versions of Rust have suggestions how to fix your code, which may get you out of borrow checker fight.