Rust Advanced Lesson: Difference between revisions

From regional-training
No edit summary
Line 1: Line 1:
[[image:comparative-energy.png]]
[[image:mandlebrot-game.png]]<ref>mandlebrot benchmarksgame https://en.wikipedia.org/wiki/Comparison_of_programming_languages</ref><ref>mandlebrot benchmarksgames https://benchmarksgame-team.pages.debian.net/benchmarksgame/performance/mandelbrot.html</ref>
Rust<ref>rust language https://www.rust-lang.org/</ref> is a compiled semantically enhanced modern [[programming language]] with [[syntax]] similar to [[C++]]<ref>C++ https://cplusplus.com/</ref> that provides the built-in cargo<ref>rust cargo https://doc.rust-lang.org/book/ch01-03-hello-cargo.html</ref> package manager, modules<ref>rust modules https://doc.rust-lang.org/reference/items/modules.html</ref> and the rustup<ref name="rustup">rustup https://rustup.rs</ref>  tool chain installer. Rust is suited for embedded processing, fault-tolerant mission critical systems, concurrent processing with channels<ref>rust channels https://doc.rust-lang.org/rust-by-example/std_misc/channels.html</ref> for transferring values between threads, web-development, and it provides package abstraction and import, various pointer<ref>rust pointers https://steveklabnik.com/writing/pointers-in-rust-a-guide</ref><ref>rust smart poitners https://doc.rust-lang.org/book/ch15-00-smart-pointers.html</ref> implementations, including references<ref>rust references borrowing https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html</ref>, raw pointer<ref>rust raw pointer https://doc.rust-lang.org/std/primitive.pointer.html</ref>, owned pointer and the borrowed<ref>rust borrowed pointer https://doc.rust-lang.org/std/borrow/trait.Borrow.html</ref> pointer, reborrowing <ref>rust reborrow https://github.com/rust-lang/reference/issues/788</ref>, generic types, powerful traits<ref>rust traits https://doc.rust-lang.org/book/ch10-02-traits.html</ref>, allocators<ref>Rust allocators https://doc.rust-lang.org/std/alloc/trait.Allocator.htm</ref>, including no-heap allocation, closures<ref>rust closures https://doc.rust-lang.org/book/ch13-01-closures.html</ref>, mutable<ref>rust mutable https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html</ref><ref>rust mutable https://doc.rust-lang.org/stable/rust-by-example/scope/borrow/mut.html</ref> and fearless concurrency<ref>rust feerless concurrency https://doc.rust-lang.org/book/ch16-00-concurrency.html</ref> paradigm, and closures<ref>rust closures https://doc.rust-lang.org/book/ch13-01-closures.html</ref>. Like C++ it has destructors<ref>rust destructors https://doc.rust-lang.org/stable/reference/destructors.html</ref> via Drop<ref>rust Drop trait https://doc.rust-lang.org/rust-by-example/trait/drop.html</ref> scope as well as control over allocation and will be imminently suitable for resource management. Rust also supports operator<ref>Rust operators https://doc.rust-lang.org/book/appendix-02-operators.html</ref> overloading<ref>Rust operator overloading https://doc.rust-lang.org/rust-by-example/trait/ops.html</ref> via traits. I expect that rust will displace C++ as the language of choice for embedded and complex system development.  
Rust<ref>rust language https://www.rust-lang.org/</ref> is a compiled semantically enhanced modern [[programming language]] with [[syntax]] similar to [[C++]]<ref>C++ https://cplusplus.com/</ref> that provides the built-in cargo<ref>rust cargo https://doc.rust-lang.org/book/ch01-03-hello-cargo.html</ref> package manager, modules<ref>rust modules https://doc.rust-lang.org/reference/items/modules.html</ref> and the rustup<ref name="rustup">rustup https://rustup.rs</ref>  tool chain installer. Rust is suited for embedded processing, fault-tolerant mission critical systems, concurrent processing with channels<ref>rust channels https://doc.rust-lang.org/rust-by-example/std_misc/channels.html</ref> for transferring values between threads, web-development, and it provides package abstraction and import, various pointer<ref>rust pointers https://steveklabnik.com/writing/pointers-in-rust-a-guide</ref><ref>rust smart poitners https://doc.rust-lang.org/book/ch15-00-smart-pointers.html</ref> implementations, including references<ref>rust references borrowing https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html</ref>, raw pointer<ref>rust raw pointer https://doc.rust-lang.org/std/primitive.pointer.html</ref>, owned pointer and the borrowed<ref>rust borrowed pointer https://doc.rust-lang.org/std/borrow/trait.Borrow.html</ref> pointer, reborrowing <ref>rust reborrow https://github.com/rust-lang/reference/issues/788</ref>, generic types, powerful traits<ref>rust traits https://doc.rust-lang.org/book/ch10-02-traits.html</ref>, allocators<ref>Rust allocators https://doc.rust-lang.org/std/alloc/trait.Allocator.htm</ref>, including no-heap allocation, closures<ref>rust closures https://doc.rust-lang.org/book/ch13-01-closures.html</ref>, mutable<ref>rust mutable https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html</ref><ref>rust mutable https://doc.rust-lang.org/stable/rust-by-example/scope/borrow/mut.html</ref> and fearless concurrency<ref>rust feerless concurrency https://doc.rust-lang.org/book/ch16-00-concurrency.html</ref> paradigm, and closures<ref>rust closures https://doc.rust-lang.org/book/ch13-01-closures.html</ref>. Like C++ it has destructors<ref>rust destructors https://doc.rust-lang.org/stable/reference/destructors.html</ref> via Drop<ref>rust Drop trait https://doc.rust-lang.org/rust-by-example/trait/drop.html</ref> scope as well as control over allocation and will be imminently suitable for resource management. Rust also supports operator<ref>Rust operators https://doc.rust-lang.org/book/appendix-02-operators.html</ref> overloading<ref>Rust operator overloading https://doc.rust-lang.org/rust-by-example/trait/ops.html</ref> via traits. I expect that rust will displace C++ as the language of choice for embedded and complex system development.  



Revision as of 09:09, 11 September 2023

[1][2]


Rust[3] is a compiled semantically enhanced modern programming language with syntax similar to C++[4] that provides the built-in cargo[5] package manager, modules[6] and the rustup[7] tool chain installer. Rust is suited for embedded processing, fault-tolerant mission critical systems, concurrent processing with channels[8] for transferring values between threads, web-development, and it provides package abstraction and import, various pointer[9][10] implementations, including references[11], raw pointer[12], owned pointer and the borrowed[13] pointer, reborrowing [14], generic types, powerful traits[15], allocators[16], including no-heap allocation, closures[17], mutable[18][19] and fearless concurrency[20] paradigm, and closures[21]. Like C++ it has destructors[22] via Drop[23] scope as well as control over allocation and will be imminently suitable for resource management. Rust also supports operator[24] overloading[25] via traits. I expect that rust will displace C++ as the language of choice for embedded and complex system development.

Rust also provides the unit type and the usual array and the additional compound types tuples[26].

Rust provides rustup[7] for tool-chain management and supports cross-compilation[27] for many platforms - thus providing good portability. I can see it being very useful in the world of prolific IoT devices, as well as in large complex systems.

Rust has several call-by mechanisms and entrenched with this is object lifetimes[28] management and access that is enforced by the compiler:

// Examples of methods implemented on struct `Example`.
struct Example;
type Alias = Example;
trait Trait { type Output; }
impl Trait for Example { type Output = Example; }
impl Example {
    fn by_value(self: Self) {}
    fn by_ref(self: &Self) {}
    fn by_ref_mut(self: &mut Self) {}
    fn by_box(self: Box<Self>) {}
    fn by_rc(self: Rc<Self>) {}
    fn by_arc(self: Arc<Self>) {}
    fn by_pin(self: Pin<&Self>) {}
    fn explicit_type(self: Arc<Example>) {}
    fn with_lifetime<'a>(self: &'a Self) {}
    fn nested<'a>(self: &mut &'a Arc<Rc<Box<Alias>>>) {}
    fn via_projection(self: <Example as Trait>::Output) {}
}
  • pass by reference is a "lend" of the pointer
  • pass by mutable reference permits the function to modify the referenced object
  • pass by box means the the Boxed item will be deallocated when the function goes out of scope. A Box implements Drop and owns its pointer
  • Rc is a smart pointer that implements a reference count; the reference count is increased on each call to a function or when cloned, until the object is no longer referenced by code.
  • Arc[29] is an atomically referenced counted object and may be used in multithreaded context.

So I am going to trial switching critical system development over to this new language platform. I can't wait to put it to use and I already have bunches of ideas in the backlog.

Rust uses copy/destroy move[30] when passing parameters; the old variable reference is no longer valid after such a move, and access constraint will be enforced by the compiler to prevent accessing the original value that was moved. Here is a brief explanation of the call by semantics:

  • the Rust provision is described in the rustbook[31]
  • cargo is described in the cargo[32] book
  • the rust language is described in the rust language book[33]
  • the rustc compiler is described in its own reference[34]

The standard library is described in the std[35] cargo reference.

rust ideas

reference code

Example code[36] is provided https://doc.rust-lang.org/stable/rust-by-example/

Plus there are other interesting aspects:

example codes

platforms

references

  1. mandlebrot benchmarksgame https://en.wikipedia.org/wiki/Comparison_of_programming_languages
  2. mandlebrot benchmarksgames https://benchmarksgame-team.pages.debian.net/benchmarksgame/performance/mandelbrot.html
  3. rust language https://www.rust-lang.org/
  4. C++ https://cplusplus.com/
  5. rust cargo https://doc.rust-lang.org/book/ch01-03-hello-cargo.html
  6. rust modules https://doc.rust-lang.org/reference/items/modules.html
  7. 7.0 7.1 rustup https://rustup.rs
  8. rust channels https://doc.rust-lang.org/rust-by-example/std_misc/channels.html
  9. rust pointers https://steveklabnik.com/writing/pointers-in-rust-a-guide
  10. rust smart poitners https://doc.rust-lang.org/book/ch15-00-smart-pointers.html
  11. rust references borrowing https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html
  12. rust raw pointer https://doc.rust-lang.org/std/primitive.pointer.html
  13. rust borrowed pointer https://doc.rust-lang.org/std/borrow/trait.Borrow.html
  14. rust reborrow https://github.com/rust-lang/reference/issues/788
  15. rust traits https://doc.rust-lang.org/book/ch10-02-traits.html
  16. Rust allocators https://doc.rust-lang.org/std/alloc/trait.Allocator.htm
  17. rust closures https://doc.rust-lang.org/book/ch13-01-closures.html
  18. rust mutable https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html
  19. rust mutable https://doc.rust-lang.org/stable/rust-by-example/scope/borrow/mut.html
  20. rust feerless concurrency https://doc.rust-lang.org/book/ch16-00-concurrency.html
  21. rust closures https://doc.rust-lang.org/book/ch13-01-closures.html
  22. rust destructors https://doc.rust-lang.org/stable/reference/destructors.html
  23. rust Drop trait https://doc.rust-lang.org/rust-by-example/trait/drop.html
  24. Rust operators https://doc.rust-lang.org/book/appendix-02-operators.html
  25. Rust operator overloading https://doc.rust-lang.org/rust-by-example/trait/ops.html
  26. rust primitives https://doc.rust-lang.org/stable/rust-by-example/primitives.html
  27. rust cross-compilation https://rust-lang.github.io/rustup/cross-compilation.html
  28. rust lifetimes https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/book/first-edition/lifetimes.html
  29. rust Arc https://doc.rust-lang.org/std/sync/struct.Arc.html
  30. rust move semantics https://stackoverflow.com/questions/29490670/how-does-rust-provide-move-semantics
  31. rust book https://doc.rust-lang.org/stable/reference/introduction.html
  32. cargo book https://doc.rust-lang.org/cargo/
  33. rust language book https://doc.rust-lang.org/stable/book/index.html
  34. rustc https://doc.rust-lang.org/stable/rustc/index.html
  35. std cargo https://doc.rust-lang.org/stable/std/index.html
  36. rust code examples https://doc.rust-lang.org/stable/rust-by-example/