Is there a way to learn the programming concepts to make it easier to learn Rust?

I am struggling to learn Rust. I believe that Rust is a fantastic programming language. It has very interesting concepts such as immutability by default, data race protections, memory leak protections etc. But there are just things that make it difficult for me to get a grasp on.

I was wondering are there any good resources to teach me how to get a grasp on all the programming concepts like static lifecycles, the stack and the heap memory usage, multithreading etc? Something that will really help me learn Rust by a lot?

1 Like

Take a look at the book "The Rust Programming Language". It is also available as a printed book.

2 Likes

Not really a direct answer, but after learning some of the basic syntax (the book is a great place to start, although it's not recommended to hit the book with 0 knowledge), you might wanna look through this.

3 Likes

That is a very good question.

I cannot be of much help. Back in the day notions of stack, heap and static data were all part of programming 101. Especially as we started programming in assembly language where you had to take care of where every byte of data was, and often a stack is directly supported by instructions in the processor. As are pointers to data that may live elsewhere. So when we came to languages like C it was natural to understand that local variables in functions were on the stack, data obtained by malloc was on the heap and string literals were in a static area or memory.

I wonder if it is really necessary to understand 'stack` to learn Rust. At least to the level we did.

What is required to understand is that in a function, for example:

fn some_func () {
    let x = 2;
    let y = 3;
    ... 
}

then those variables, x and y are come into existence when the function starts to run, when it is called, and cease to exist when the function stops running, when it returns to the caller.

With that in mind one can immediately get the idea that returning a reference to x or y is not going to work, when the function ends x and y are gone, the reference would then refer to nothing (or at least some random junk in memory wherever they were)

Does one really need to understand stack in order to understand the above?

I guess I could continue in this vein to try and explain longer lived data items, on the heap, without requiring the notion of 'heap`, although I can't be sure the explanation world work...

I might suggest forgetting about threads for a while.

5 Likes

I think it would be very helpful if there were explanations of stack, heap, threads and the like that were accompanied by some nice big clear diagrams of memory and where data is in that memory and how things get added to / removed from the stack, inserted/deleted from the heap etc.

I have never seen such graphical explanations in the context of teach Rust.

4 Likes

The book convers the stack and the heap:

https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html#the-stack-and-the-heap

So it does.

It already seems to be pitched at a higher level that the OP's question is at.

It lacks those baby steps in explaining stack and heap together with nice diagrams and a little walk through example of how they work as functions are called and returned, local variables come in and out of scope etc.

1 Like

Well yeah but isn't that more or less general programming concepts as opposed to being Rust specific? I perceived OPs question about Stacks and Heaps as about how and why Box<> puts stuff on the Heap.... Why it can't be put on the Stack because they aren't Sized etc, more rust specific things basically. I think the book covers those fairly well enough.

2 Likes

Yes they are. And that is exactly the point I am trying to make.

Anyone new to programming who tries to get into it with Rust as a first language, or anyone whose only programming experience is with Python, Java, whatever language hides all those low level details, will need those "general programming concepts" explained to them before they can make any sense of anything in Rust.

So my thought is to have those basic programming concepts explained, at a level pitched for beginners, but in the context of Rust. Using Rust examples and supporting diagrams etc. Rather than, say, C.

Interestingly you have referred to notions of stack, heap etc as "general programming concepts". Which they are from our perspective. From the perspective of many who have been coding in Python or whatever, they are very likely new ideas, even if they have done a lot of Python, or whatever, for some time.

1 Like

True, these are things I know about from University education. These are not things I've ever had to think about in practical use cases before rust. I can imagine these would be completely alien ideas to someone who would try to start off with rust or coming from more forgiving things like python or js :sweat_smile:

1 Like

You can also learn a language where you have to do these manually and pay attention to memory safety, eg. C or C++. I was a long-time C++ programmer before I switched to Rust, and it had basically no learning curve for me.

1 Like

That is an interesting perspective.

What you described above as "general programming concepts" turns out to be what you learned as part of a university education.

I would posit that is not so "general" then.

Until recent years most of the worlds programmers never had a CS education in a university, or else where, there was hardly such a thing. Undergraduate CS courses were a rare thing. I suspect most still don't.

As you say "not things I've ever had to think about in practical use". That is the situation many programmers are in today, with their Python and Java. Except unlike you they likely don't have that university CS education behind them in case they need it, like when adopting Rust.

1 Like

Oh that is good. Thanks mate.

I have little experience with C/C++. Is the syntax of C/C++ similar to Rusts?

Yes, but the syntax is the easiest part. The more important point is that C++ has many similar concepts, e.g. ownership, it's just that you have to take care of it manually, so you have to understand how it works, otherwise your programs will have undefined behavior.

2 Likes

One problem with diagrams for new programmers is that different CPU architectures use different stack memory layouts. Thus a diagram of a stack growing up with increasing memory addresses is likely to confuse someone who is trying to write embedded code for an architecture where the stack grows down with decreasing memory addresses.

Very true.

I doubt that is a detail that matters when introducing beginners to the idea.

Presumably when they get to messing with real CPU stacks on embedded systems or wherever they are not beginners anymore.

1 Like

But @Joe232, the OP for this thread, is attempting embedded programming and he does seem to be a beginner, at least from the vantage of people such as you and me who cut their teeth decades ago on assembly-level bare-metal programming.

It can be done.

The first machine I ever programmed that was physically in my presence, rather than on the end of a long telephone link, I programmed in assembler. Assembled by hand, with pencil on paper, to hex codes loaded into EPROM. Had to build the machine first mind. Using a Motorola 6809 and the usual cast of parts. It was great.

That is the way to get a feel for stacks and heap and such, CS education not required. :slight_smile:

1 Like

I wish I could have had that experience.

1 Like