Aha re Rust's borrow checker

41 views
Skip to first unread message

Edward K. Ream

unread,
Oct 5, 2024, 8:45:04 AM10/5/24
to leo-editor

Lately, I've been studying Rust using my (tbo-in-rust) project. This toy project transliterates Leo's token-based beautifier (leoTokens.py) into Rust.


Yesterday, I discovered a straightforward pattern that keeps Rust's borrow checker happy! This post discusses this pattern. It's both a design and coding pattern.


The pattern: Pass only immutable arguments


Methods create mutable data, but when those methods return, the rest of the program treats those data as immutable. For example, here is the beautifier's main line:


// Read the file into contents (a String).

let contents = fs::read_to_string(file_name)

  .expect("Error reading{file_name}");

// Create (an immutable!) Vec of InputToks.

let input_tokens = self.make_input_list(&contents);

// The prepass creates (an immutable!) Vec of AnnotatedInputToks.

let annotated_tokens = self.annotate_tokens(&input_tokens);

// Beautify.

let results = self.beautify(&annotated_tokens);


This pattern confines mutable references within individual methods.


Aha: this pattern enforces functional programming.


Summary


Other coding patterns might keep the borrow checker happy, but a functional style seems reasonable and good.


Learning Rust has suddenly accelerated! I'm about to lose my newbie status.


Edward


P.S. The tbo-in-rust project will likely remain a toy project because the transliterated beautifier won't be significantly faster than the Python code! The culprit is RustPython's tokenizer module. It's slower than Python's!


EKR

Edward K. Ream

unread,
Oct 5, 2024, 3:08:35 PM10/5/24
to leo-e...@googlegroups.com

> Other coding patterns might keep the borrow checker happy, but a functional style seems reasonable and good.
> Learning Rust has suddenly accelerated! I'm about to lose my newbie status.

Heh. Not yet. The borrow checker is still running wild :-) More learning to come.

Edward

Edward K. Ream

unread,
Oct 9, 2024, 9:36:57 AM10/9/24
to leo-editor
On Saturday, October 5, 2024 at 7:45:04 AM UTC-5 Edward K. Ream wrote:

> Lately, I've been studying Rust using my (tbo-in-rust) project. This toy project transliterates Leo's token-based beautifier (leoTokens.py) into Rust.

The updated readme file discusses the status of the tbo-in-rust project. The project is at a good stopping point, but I might press on a bit more.

Here are a few notes:

Aha: In structs, use pointers, not objects where possible.

This pattern requires lifetime annotations. Happily, the compiler usually suggests the correct hieroglyphics! This pattern usually keeps the borrow checker happy.

Optional data is a nightmare in function/method signatures.

 My workaround is to use dummy entries in data structures to avoid using "None".

It's shocking how much cruft Rust has.

After a few days I lost sight of all the blah-blah-blah that Rust requires. When I started to transliterate another piece of Python code, it was shocking how much more clearly Python expresses ideas.

Summary

- As reported earlier, the tbo-in-rust project will never be fast enough to be useful as production code.
- Using the patterns reported here, programming in Rust looks and feels much like programming in C.
- I'm glad to have learned the basics of Rust, but I have no interest in becoming a Rust programmer.

Edward

Edward K. Ream

unread,
Oct 9, 2024, 11:33:26 AM10/9/24
to leo-editor
On Saturday, October 5, 2024 Edward K. Ream wrote:

> Lately, I've been studying Rust using my (tbo-in-rust) project.

Here are some more (perhaps last) thoughts about Rust.


Rust's language design and compiler are works of genius. The compiler often suggests the correct fix! It's been fun experiencing Rust's superb engineering.


But Rust is a language I'll admire but won't ever love. Rust excels at low-level systems programming, including Python's infrastructure. For everything else, Python expresses ideas far more clearly and with far less work.


Edward

Edward K. Ream

unread,
Oct 9, 2024, 5:59:17 PM10/9/24
to leo-editor
On Wednesday, October 9, 2024 at 8:36:57 AM UTC-5 Edward K. Ream wrote:

The updated readme file discusses the status of the tbo-in-rust project. The project is at a good stopping point, but I might press on a bit more.

I've resolved the last language-related question!

I wanted to define index_dict as HashMap<usize, &'a str> but the borrow checker complained that the context argument to set_context did not live long enough.

However, the context arg is always a slice, so I only needed to change the signature from:

     fn set_context(&mut self, i: usize, context: &str) {
to:
    fn set_context(&mut self, i: usize, context: &'static str) {

So it looks like my study of Rust is complete for now.

Edward
Reply all
Reply to author
Forward
0 new messages