Rust Advanced Lesson: Difference between revisions

From regional-training
No edit summary
Line 76: Line 76:
[[category:Programming Language]]
[[category:Programming Language]]
[[category:glossary]]
[[category:glossary]]
[[category:index]]

Revision as of 13:24, 28 April 2023

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

Rust provides rustup[5] for tool-chain management and supports cross-compilation[25] 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[26] 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[27] 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[28] 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[29]
  • cargo is described in the cargo[30] book
  • the rust language is described in the rust language book[31]
  • the rustc compiler is described in its own reference[32]

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

rust ideas

reference code

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

Plus there are other interesting aspects:

example codes

platforms

references

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

categories