5 ms·
This is consistent with my observations of porting Java code to Rust. Much simpler and nicer to read safe Rust code (no unsafe tricks) compiles to programs that
by pkolaczk 7y ago
This is consistent with my observations of porting Java code to Rust. Much simpler and nicer to read safe Rust code (no unsafe tricks) compiles to programs that outperform carefully tuned Java code.
- marta_morena 7y agoSorry, but `Much simpler and nicer` is something that I highly doubt when you talk about Java to Rust. Unless the people writing the Java code were C programmers, lol, in which case I feel for you.
- efaref 7y agoRust's type system is more expressive than Java's so you can end up with much nicer to read code with stricter and more obvious invariants. There also tends to be way less of the `EnterpriseJavaBeanFactory`-style code in idiomatic Rust.
- Polyisoprene 7y agoTrolling is fun and all, but I wouldn’t say Rust’s type system that much more advanced than Java’s. The borrow checker definitely helps to catch errors, but I would rate them at basically the same level.
- gameswithgo 7y agoSo, being able to have a value or a reference vs everything always being a reference is a pretty massive difference. Option types instead of nulls is also a pretty large difference. Generics being better from a performance perspective is a large difference too. As well, traits are quite a bit different than classes, but not always in a good way!
- Polyisoprene 7y agoHopefully Java will support proper values regarding inlining and on-stack allocation for non-primitive types soon, but yes option types are a benefit in Rust as long as you’re not using any more advanced reference libraries.
- tene 7y agoCan you explain more about "advanced reference libraries", and how they interfere with the benefits of option types?
- rattray 7y ago> Trolling is fun and all This isn't in keeping with HN's guidelines[0], eg: > Be kind. Don't be snarky. Of course, you may be expressing an unpopular opinion, so your comment may receive downvotes regardless. You may wish to delete and repost without the first clause to more accurately gauge community response. https://news.ycombinator.com/newsguidelines.html https://news.ycombinator.com/newsguidelines.html
- Polyisoprene 7y agoSure, just reflecting on the “There also tends to be way less of the `EnterpriseJavaBeanFactory`-style code in idiomatic Rust.”
- roca 7y agoRust's type system is definitely much more powerful, even ignoring borrowing. Rust's affine (ownership) types add a lot of power. They make it possible for APIs to take ownership of a passed-in object and guarantee no other references to it exist. For example this lets you manually deallocate resources (e.g. close a File), while preserving the invariant that if you have a reference to a File, then it is open. Also, Rust traits and generics are a lot more powerful than anything Java has. For example Rust generics support associated types. E.g. the Iterator trait has an associated type Item: trait Iterator { type Item; ... } You can now write code that's generic over Iterator, and refers to its Item type: fn first<I: Iterator>(iter: I) -> I::Item { iter.next().unwrap() } Toy example, but associated types are really important. Rust traits and generics are more powerful in other ways too. E.g. in Rust you can do anything with a generic type, unlike Java where type erasure means you can't write 'new T' etc.
- roca 7y agoBTW I'm not knocking Java here. Java's simpler type system is actually great for the applications I think Java is good for --- business logic and simple mobile apps, the COBOL of the 21st century. Well, except that Java went ahead and gratuitously complicated their type system with wildcards. That was crazy.
- Polyisoprene 7y agoSure, but generics are available in Java as well and compared to Haskell and similar language I rate Rust as closer to Java.
- jeremysalwen 7y agoRust's generics are much more powerful. You can do type level programming, e.g. : https://docs.rs/peano/1.0.2/peano/ https://docs.rs/peano/1.0.2/peano/ There's an interesting comparison of Rust and Haskell's type system here: https://www.reddit.com/r/rust/comments/4jh8hv/question_about_rust_vs_haskell_type_systems/ https://www.reddit.com/r/rust/comments/4jh8hv/question_about... I guess it's just a matter of opinion about what sorts of differences you think are significant vs not.
- 7y ago
- gameswithgo 7y agoRemember that the comparison is a hand performance tuned Java program, vs a naive Rust implementation. When you are trying to extract performance out of Java, things can get pretty messy.
- echelon 7y agoI write Java during the day but wish I could write Rust instead. Rust has a much nicer standard library, and the semantics and abstractions are so incredibly beautiful. It's a language that has learned from the millions of human-years that went into other languages and ecosystems. Every corner and seam in the language design speaks to this. Traits, union type enums, pattern matching, option and result types, derive(), error handling semantics - it's all incredibly intuitive and expressive. If I had a choice to write Rust everywhere, I would.
- millstone 7y agoRust has its share of random-feeling nonsense, like requiring PhantomData to "work around" unused type params, or that you can't compare arrays longer than 32 elements.
- gridspy 7y ago1. I've never had to use PhantomData since I started coding in rust pretty much fulltime 2 years ago. 2. You can compare arrays longer than 32, but the compiler will no longer create an automatic comparison for you. So it's not that you "can't" do it. -- That said, using == to compare >32 elements sounds inefficient, perhaps check your use-case?
- xmprt 7y agoNot comparing arrays longer than 32 elements by == sounds like a feature to me.
- millstone 7y agoThe 32-count cliff is not just for Eq - it also defeats niceties like Debug, Hash, Default. This isn't a deliberate design decision, it's due to a (current) limitation of Rust's value-type generics. The point is to illustrate that when you WTF using Rust, sometimes it's not you, it really is Rust.
- lumost 7y ago
- dang 7y agoWe detached this subthread from https://news.ycombinator.com/item?id=22240978 https://news.ycombinator.com/item?id=22240978.