Go Vs Rust

Hi:

I have been coding in Go, and recently I heard about Rust. As I see Go also has almost same features as Rust like - functional language constructs, great concurrent primitives, optional GC, easy to use, systems language.

Hence would like to understand why would one choose Rust over Go. Please help me understand.

Thanks.

  1. Rust is more of an actual systems language than Go. So it actually doesn’t use or need a GC at all while Go depends on a GC to clean up the garbage that gets generated. People have been able to successfully use Rust in a variety of embedded environments and in kernels, which Go is simply not suited for at all.
  2. Rust is a much safer language. Things like not having null pointers, and achieving memory safety without a GC are things that Rust can do that Go does not do.
  3. Sugar for user-defined types. You can define your own custom containers and have operators like [] indexing on them, unlike Go which only allows operators for the built in types. Also various operators for arithmetic stuff.
  4. Things like generics and traits to allow type-safe and efficient code that takes a variety of types. Go meanwhile just has interfaces which have no compile time checks. If you write a “generic” container in Go there’s nothing stopping you from inserting something of the wrong type.
2 Likes

I think you mean interface{} has no compile time checks. Certainly, non-empty interfaces do. (The compiler will ensure that a type T for interface I actually satisfies interface I. All types satisfy interface{}.) This means that Go does indeed have type safe polymorphism (structural subtyping). You will sacrifice static dispatch in the process, though.

2 Likes

Although Go’s polymorphism solution is arguably a bit less type-safe because it’s duck-typed. iirc this led to some frustating results when their std lib started to optimistically up(down?)cast interfaces for specialization purposes. Essentially checking if there also happened to be another method on the input with the right name and calling that.

Yay, tradeoffs!

This was also discussed recently on reddit.

For an opinion from the other side, one of original Go developers Ian Lance Taylor answered this question.

https://www.quora.com/What-do-Go-programming-language-designers-think-of-Rust-programming-language

1 Like

Thanks for the reply. If GC is optional in Rust, then how does it do memory management? Is it same as in C with some functions like malloc() and free(). Then wouldnt we again run the risk of memory leaks or buffer overuns or so since we re managing memory? Of course, we do get full control of memory management as in C.

Thanks, this article of reddit is quite good one.

Thanks.

I’d recommend reading the rust book, which explains ownership and memory management in Rust more completely.

https://doc.rust-lang.org/stable/book/

It's not even optional, tracing GC is entirely nonexistant.

(It seems like you've found material for the rest)

1 Like

Memory management in Rust is more like memory management in modern C++, but with enhancements to be fully safe. You use the RAII pattern and move semantics to acquire, release, and transfer ownership of memory allocations, and you use borrows to pass temporary references to it to code that just needs to have a reference to work with but doesn't need to own the value.

Rust is a lot more like chess than it is like go…

I have an argument that is rarely mentioned: Go like Python takes inspiration by one (Benevolent Dictator For Life). Rust is true community product. I feel the language progresses faster with more practical direction. But it’s really hard to compare it with Go.

Unsophisticated comparsion: with Rust you can write and improve Go runtime (GC, etc.), but it’s harder vice versa.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.