Difficult to Understand Some Concepts in Rust

This morning, I read a Rust book that I borrowed from the library.

I am having difficulty understanding some concepts, such as:
Box
dyn
Mutex
Deref and DerefMut
Drop
Fn, FnMut, and FnOnce

And so on.

However, I believe it's okay that I don't understand them yet. Similar to 'unsafe', which I am still struggling to grasp, I hope that one day in the future, after several years of practice, I will be able to understand them.

That can be. I have been using Rust for over 4 years now, I have services critical to our business in Rust that have been running for a lot of that time. I have never used any of those items on your list save for mutex.

5 Likes

I understand. Thank you very much. So far, I haven not the opportunity to get a new job involving coding. With my hearing impairment, it's difficult to find new job opportunities.

As @ZiCog said, the surface of a programming language can be so huge as to have features you would probably never use. This is particularly true for Rust, since it's aimed to cater both the high-level and low-level programming niches.

2 Likes

I understand, thank you very much.

Probably with the exception of dyn, there isn't much of a "concept" to these. Box is just a heap-allocated pointer, a mutex is a mutex in any language, and the rest are just traits corresponding to some pretty trivial behavior. Deref and DerefMut are just the desugared version of the dereferencing operator, Fn* traits are implemented by callable, function-like things, and Drop is how you do destructors.

Specifically what do you not understand about these? You may be overthinking.

3 Likes

Depending on peoples backgrounds just that simple sentence could be laden with new concepts. What is "heap"? What is "allocated"? Heck, what is a "pointer"? There are those who could have been programming successfully for years and never had to know or care about such things.

13 Likes

None of those are specific to Rust.

If you don't know what a pointer is, then you are not "having difficulty understanding Rust concepts". Then you are lacking CS basics that would be required for being fluent in any language.

The sort of question OP is asking is not helpful precisely because of this generality or potential depth. If we can't assume any sort of general background, the we stand no chance of explaining more specific or advanced topics. This is why I'm asking what specifically what they don't understand. Only that is a feasible learning/teaching strategy.

If someone told you they don't understand compound interest, would you rather say "it's just interest on the capital already augmented with any previous interests", or would you start by assuming the person doesn't know about money, percentages, or multiplication and addition? The latter is just not realistic, given the nature and context of the question.

4 Likes

It's true that none of these concepts are specific to Rust and one would expect them as general knowledge of CS basics. Although it seems to me that there is likely an ocean of people making a living programming who have never had to concern themselves with such things. Perhaps there background is not in CS and they have been using languages like Javascript and Python to get what they want done. I can well imagine that group finds talk of "heap", "stack", "allocation", "pointer" quite alien.

Re: your compound interest example, Give the number of people I meet that have a flimsy grasp of percentages and can barely do arithmetic without a calculator/computer I think it might be quite realistic to assume that if they ask about compound interest.

Anyway, yes, it would be easier to deal with specific questions about specific language features.

7 Likes

That's actually rather strange that you've never used eg. box or drop in four years! Do you use a lot of Arc instead? Technically those use Drop, but not manually implementing it is a reasonable interpretation of "not using" Drop. Or do you simply never allocate at all? Impressive if so.

(Just to clarify because it's the internet; not a criticism, everyone has different uses)

2 Likes

I would note that you don't have to understand these things to use them, and use them well. It's perfectly fine to simply know that where you see Fn, FnMut or FnOnce in a type signature, you can put a closure there, without any understanding of why, and just using trial-and-error to make the closure Fn or FnMut if needed.

Once you've used it enough, you'll have an intuition for what it means, and then you can build on that to establish a proper understanding.

7 Likes

Perhaps "never" is overstating it. I just checked, I have a few cases of boxing errors in some recent code, no cases of "drop" though. I have a couple of uses of Arc but mostly my threads exchange data over channels, and looking at it now I think my user of Arc in those cases is misguided. Of course there must be lots of allocation going on in the Vecs and such I use.

2 Likes

I would like to take an opportunity to say that it’s perfectly fine to be confused as a beginner. There are many of us here on the forum who have already put in our “10,000 hours” for expert level programming. Nearly all of things on your list are concepts that none of us knew the first moment they were introduced.

On the other hand, some of these are Rust-isms for concepts known generally by other names. “Box” is an uncommon name for a heap allocation. Deref can be thought of as an operator overload in another language. And so on.

My suggestion is to start slowly. You do not need to jump into all subjects at once. Write simple things and build complex things out of many simple things. Learn a new concept when you need it, or just as a daily study to keep yourself from being overwhelmed.

Over time you will build a lexicon to work with each of these things and more. But having them explained one-by-one on the forum is very different from deep diving into any one subject on your own. It’s the difference between being given a fish and learning how to catch a fish.

10 Likes

Thank you for your thoughtful response and kind words of encouragement! I appreciate your advice on how to approach Rust as a beginner. You're right that being confused is perfectly fine, and I'm glad to know that even experts had to put in many hours to reach their current level.

I find your explanation of Rust-specific concepts in relation to other languages very helpful. It's reassuring to know that "Box" and "Deref" are just Rust's way of representing more general ideas.

Your suggestion to start slowly and learn new concepts as I need them is invaluable. I will definitely take this approach and focus on writing simple programs first. I agree that learning a little every day will help me avoid feeling overwhelmed.

Thank you again for your time and your generous advice. I'm looking forward to applying your suggestions and continuing my journey in Rust.

Best regards.

3 Likes

Thank you for sharing your insights and advice! I wholeheartedly agree with your perspective that as beginners, we don't necessarily need to fully understand the underlying principles of every concept to use them effectively. As you mentioned, simply knowing that where we see Fn, FnMut, or FnOnce in a type signature, we can place a closure there without delving into the whys is sufficient. Through trial and error, we can gradually learn when to use Fn or FnMut.

Your advice reminds me that cultivating intuition through practice is crucial. Over time, as I gain more experience, I will be able to build a deeper understanding of these concepts.

Once again, I appreciate your generosity in sharing your thoughts. I will definitely take your advice and continue to explore the world of Rust.

Best regards.

1 Like

Hello,

I appreciate your feedback, and I understand that my questions may seem too general to some. However, as a beginner in Rust and programming, I find it helpful to ask about the concepts I encounter as I progress through my learning journey.

You are correct that some of these topics, like pointers, mutexes, and traits, are fundamental computer science concepts. However, Rust's unique approach to them, along with its specific syntax and terminology, are still new to me. I'm still building my foundation and familiarity with the language.

Your advice to focus on specific areas of confusion is valuable. I will try to be more targeted in my inquiries as I continue to learn. Thank you for the suggestion.

I appreciate the Rust community's support for beginners. I am grateful for the patience and guidance that experienced programmers are willing to offer. I am committed to learning Rust and developing my programming skills, and I value the input I receive from more experienced developers.

Thank you again for taking the time to share your thoughts.

Best regards.

2 Likes

That is interesting. I don't recall ever implementing a Drop, but I'm using Box on an almost daily basis, a whole bunch of dyn and fairly regularly Fn and friends.

2 Likes

Okay, I see, Thank you.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.