Mar 24, 2019

[Rust] A skim through


During the afternoon skimmed through a video by Aaron Turon about the language Rust. (Since it's a skim through, only jot down information mentioned in the video at that time when it's recorded.)

A nice information indeed.

Rust uses some language idioms including:
  • Trait, Concept in C++(named by Alex Stepanov), Interface in Go.
  • 'channel' as Actor Model implemented as library thus using .Send/.Receive(?) for taking the message. The talk using pointer to object on heap thus no need copy, in Go you could create a channel with pointer type which have the same effect. Same as in Boost.Fiber.
  • Ownership of resource(instance on heap), including using type system to restrict the use of C++'s std::move (in C++, use the instance after move is an UB), like C++'s std::unique_ptr (assume using name mangling in type system)
  • With 'borrowing, Rust's term of resource ownership transfer/sharing', like C++'s std::shared_ptr + control block, which has reference counting implemented internally.
  • Slice, Go's slice, C++'s view. (internal data structure is just the same idea)
  • Concurrency, not mentioned much, and not mentioned memory model of the language. 
About the Rust syntax, Andrei Alexandrescu's words, quoted:
Foreign syntax. Rust's syntax is different, and there's no apparent advantage to the difference. This is irritating for folks coming from Algol-style languages, who need to deal with a gratuitously different syntax in addition to getting the resource bookkeeping right.

For now, no foreseeing project on my plate needs to use Rust, which C++/Go/C/Dart has filled in the gaps.

After note:
I really like the interface idea Go brought on the table.
It's really smart, really.
Not only it enables the 'struct' in Go becomes C's POD(which is so nice for copying the instance, no extra check and burden), lift/dissect the ptr to vtable, vtable, RTTI from 'struct' itself into 'interface'.
Interface also has this 'view' like internal layout design which makes copying it lightweight as well.
In C++, maybe Macro can mimic Go's interface by code gen base struct with virtual functions and those struct which implements it inherits this base struct at compile time.

Like Russ Cox said in the post "Go Data Structures: Interfaces", quoted:
Go's interfaces—static, checked at compile time, dynamic when asked for—are, for me, the most exciting part of Go from a language design point of view. If I could export one feature of Go into other languages, it would be interfaces.

However, C++'s de-virtualization is a win for fast run-time code since compiler can avoid the extra layer of ptr jump by direct call the right type's name mangled member function, even inline it, thus is FAST.


Reference:
Concepts: The Future of Generic Programming
http://www.stroustrup.com/good_concepts.pdf

Metaprogramming with Traits - Aaron Turon
http://aturon.github.io/academic/thesis.pdf

Channels in C++:

Proposal for a C++ channel class:

Rust channels:

Proposal: New channels for Rust's standard library:

Rust and the Blub Paradox- J. Turner

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.