About books, and Jon Gjengset about memory

⚓ Rust    📅 2025-07-27    👤 surdeus    👁️ 12      

surdeus

Warning

This post was published 128 days ago. The information described in this article may have changed.

As I might continue using Rust, at least for the next 12 months, it might make some sense to read one more book in next winter. Mara Bos's book might be still too advanced for me. The upcoming new release of Jim Blandies book might be an option -- I think his current second release is quite good, maybe one of the best Rust books -- but very detailed and a bit verbose. And then there is the book of Jon Gjengset, which is sometimes recommended. I just read a few pages at Amazon.com:

Talking About Memory

Not all memory is created equal. In most programming environments, your programs have access to a stack, a heap, registers, text segments, memory-mapped registers, memory-mapped files, and perhaps nonvolatile RAM. Which one you choose to use in a particular situation has implications for what you can store there, how long it remains accessible, and what mechanisms you use to access it. The exact details of these memory regions vary between platforms and are beyond the scope of this book, but some are so important to how you reason about Rust code that they are worth covering here.

Memory Terminology

Before we dive into regions of memory, you first need to know about the difference between values, variables, and pointers. A value in Rust is the combination of a type and an element of that type’s domain of values. A value can be turned into a sequence of bytes using its type’s representation, but on its own you can think of a value more like what you, the programmer, meant. For example, the number 6 in the type u8 is an instance of the mathematical integer 6, and its in-memory representation is the byte 0x06. Similarly, the str "Hello world" is a value in the domain of all strings whose representation is its UTF-8 encoding. A value’s meaning is independent of the location where those bytes are stored.

A value is stored in a place, which is the Rust terminology for “a location that can hold a value.” This place can be on the stack, on the heap, or in a number of other locations. The most common place to store a value is a variable, which is a named value slot on the stack.

A pointer is a value that holds the address of a region of memory, so the pointer points to a place. A pointer can be dereferenced to access the value stored in the memory location it points to. We can store the same pointer in more than one variable and therefore have multiple variables that indirectly refer to the same location in memory and thus the same underlying value.

Consider the code in Listing 1-1, which illustrates these three elements.


I had to do picture to text conversion, but I think that text segment is converted correctly.

As a non native speaker, I am not sure if I should really be happy with some of the sentences. And I don't know if he is a native speaker -- from his name I have some doubts.

Not all memory is created equal.

Is that really a good phrase? Do we really create memory? Is allocating not more correct?

A value in Rust is the combination of a type and an element of that type’s domain of values.

I do not like that sentence too much, but it is correct.

A value can be turned into a sequence of bytes using its type’s representation, but on its own you can think of a value more like what you, the programmer, meant. For example, the number 6 in the type u8 is an instance of the mathematical integer 6, and its in-memory representation is the byte 0x06.

Again, correct, but is that really a nice phrase?

The most common place to store a value is a variable, which is a named value slot on the stack.

Would that imply, that each variable is always stored on the stack? I am a bit unsure -- in the past I called an array element like a[i] a variable as well -- but I am not sure if I was correct.

We can store the same pointer in more than one variable and therefore...

Yes, that is correct. But when first reading that sentence, I got a strange feeling -- I have no real idea why. I would have typically said "We can have multiple pointers referring to the same memory region..."

Well, generally that text section is correct, and I think it was created in 2022 without any AI assistance. But still, I am not convinced that I should buy and read their book.

At the end, a funny fact. I was recently pointed to a comment in LWN.net about my own book. (That website is currently unavailable -- seems to be a low quality medium.) That person thinks, that to write an excellent Rust introduction, one would need at least a decade of Rust experience, a few possible tiny errors in a book would be a disaster, and that AI assistance for writing is general evil. I think they never looked into my book, but were sure it must be bad. Well, I am very skeptical about all the hundreds Rust books at Amazon as well, but that guy seems to be not too smart :slight_smile:
Writing an excellent Rust introduction is no rocket science: Rust is complex, and a bit difficult, but the content of beginner books, like the official tutorial, it quite trivial. What is most important is, to have a clear target audience, to dedicate a few hundred hours of your time, and to explain the stuff correct and in a way that is easy to understand for the reader -- and enjoyable to read.

3 posts - 3 participants

Read full topic

🏷️ Rust_feed