Rust Advanced Lesson: Difference between revisions
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
- rust on raspberry pi
- rust CID
- rust cargo proxy
- rust async IO for raspberry pi
- rust cargo repository mirror
reference code
Example code[34] is provided https://doc.rust-lang.org/stable/rust-by-example/
Plus there are other interesting aspects:
- HAL
- RPAL raspberry pi HAL https://github.com/golemparts/rppal
example codes
- getting started https://doc.rust-lang.org/book/ch01-00-getting-started.html
- text to morse code https://www.freecodecamp.org/news/embedded-rust-programming-on-raspberry-pi-zero-w/
- mqtt https://betterprogramming.pub/rust-for-iot-is-it-time-67b14ab34b8
platforms
- pico https://allianceforthefuture-com.ngontinh24.com/article/getting-started-with-rust-on-a-raspberry-pi-pico-part-1
- ESP32 https://docs.espressif.com/projects/esp-idf/en/latest/esp32c3/hw-reference/esp32c3/user-guide-devkitm-1.html
references
- ↑ rust language https://www.rust-lang.org/
- ↑ C++ https://cplusplus.com/
- ↑ rust cargo https://doc.rust-lang.org/book/ch01-03-hello-cargo.html
- ↑ rust modules https://doc.rust-lang.org/reference/items/modules.html
- ↑ 5.0 5.1 rustup https://rustup.rs
- ↑ rust channels https://doc.rust-lang.org/rust-by-example/std_misc/channels.html
- ↑ rust pointers https://steveklabnik.com/writing/pointers-in-rust-a-guide
- ↑ rust smart poitners https://doc.rust-lang.org/book/ch15-00-smart-pointers.html
- ↑ rust references borrowing https://doc.rust-lang.org/book/ch04-02-references-and-borrowing.html
- ↑ rust raw pointer https://doc.rust-lang.org/std/primitive.pointer.html
- ↑ rust borrowed pointer https://doc.rust-lang.org/std/borrow/trait.Borrow.html
- ↑ rust reborrow https://github.com/rust-lang/reference/issues/788
- ↑ rust traits https://doc.rust-lang.org/book/ch10-02-traits.html
- ↑ Rust allocators https://doc.rust-lang.org/std/alloc/trait.Allocator.htm
- ↑ rust closures https://doc.rust-lang.org/book/ch13-01-closures.html
- ↑ rust mutable https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html
- ↑ rust mutable https://doc.rust-lang.org/stable/rust-by-example/scope/borrow/mut.html
- ↑ rust feerless concurrency https://doc.rust-lang.org/book/ch16-00-concurrency.html
- ↑ rust closures https://doc.rust-lang.org/book/ch13-01-closures.html
- ↑ rust destructors https://doc.rust-lang.org/stable/reference/destructors.html
- ↑ rust Drop trait https://doc.rust-lang.org/rust-by-example/trait/drop.html
- ↑ Rust operators https://doc.rust-lang.org/book/appendix-02-operators.html
- ↑ Rust operator overloading https://doc.rust-lang.org/rust-by-example/trait/ops.html
- ↑ rust primitives https://doc.rust-lang.org/stable/rust-by-example/primitives.html
- ↑ rust cross-compilation https://rust-lang.github.io/rustup/cross-compilation.html
- ↑ rust lifetimes https://web.mit.edu/rust-lang_v1.25/arch/amd64_ubuntu1404/share/doc/rust/html/book/first-edition/lifetimes.html
- ↑ rust Arc https://doc.rust-lang.org/std/sync/struct.Arc.html
- ↑ rust move semantics https://stackoverflow.com/questions/29490670/how-does-rust-provide-move-semantics
- ↑ rust book https://doc.rust-lang.org/stable/reference/introduction.html
- ↑ cargo book https://doc.rust-lang.org/cargo/
- ↑ rust language book https://doc.rust-lang.org/stable/book/index.html
- ↑ rustc https://doc.rust-lang.org/stable/rustc/index.html
- ↑ std cargo https://doc.rust-lang.org/stable/std/index.html
- ↑ rust code examples https://doc.rust-lang.org/stable/rust-by-example/