Comment

Comment by roca

Rust'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.

parent roca 6 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton