I am reading the Rust book at the moment and am at chapter 7. I thought that instead of reading the whole book it makes more sense to start implementing some data structures and started with undirected graphs along with standard algorithms on them. However, the compiler throws error messages at me such as the one below and I do not have understanding about traits yet. I think I should not skip chapters as they usually build upon one another.
the trait bound `Vec<_, _>: From<{integer}>` is not satisfied
--> src/main.rs:4:30
|
4 | let adj_list = Vec::from(Vec::from(2,4), Vec::from(1,3));
what would you recommend as a good path to learn - continue with implementing or reading the whole book first?
I was trying to learn skying and decided that all these green, blue, black, or double black tracks are too boring and went with alpine touring… that's essentially what you are doing.
“Start by implementing a graph based algorithm” is a Mistake #3 FWIW.
One may work with rust for years and write dozen of apps before one may ever need to implement, from scratch, such algorithm in Rust. Which is incredibly hard, because graphs are, essentially, the hardest data structures to attempt to implement in Rust. Even developers with decade of Rust experience would think long and hard about doing that (instead of picking some already implemented version).
I generally find that trying to implement something I care about, and then looking up what I need to know to achieve that, works best.
Here, though, you might want to go (re)read the chapter on vectors from the book. I’m pretty sure that the line of code you want is this— Vec::from is more for converting another kind of list instead of constructing a new one.
let adj_list = vec![vec![2,4], vec![1,3]];
That advice is mostly to do with implementing reference-based (& or (A)rc) graph structures. There shouldn’t be any particular trouble with this sort of adjacency-matrix approach.
Thanks both of you. My goal is to have a solid understanding of Rust so that I can build software myself using it. I will try a hybrid approach of building alongside reading.
I don't know how much you already know about computer programming, but I would indeed suggest reading the whole book first. The official book is in my opinion more an extended tutorial, and one should really know all what is contained, perhaps with the exception of macros and async. Doing some small exercises while reading might be a good idea, I started with converting my tiny chess engine from Nim to Rust. Unfortunately you are already at chapter 7, otherwise I had strongly suggested you starting with my book
[EDIT]
“Start by implementing a graph based algorithm” is a Mistake #3 FWIW.
That mentioned blog looks indeed quite interesting -- I had missed it.
If you have prior programming experience, I think rewriting in Rust something you wrote in another language is very helpful. Doing so lets you to focus more on Rust itself and less on target functionalities, and you can appreciate where and how Rust asks you to think differently.
Also, read The Book carefully before starting work on an actual application. A comprehensive view of the whole "Rust problem" can save you a lot of headaches.
As much as I concur with the general sentiment, I do feel the need to play a bit of a devil's advocate here: the chance of breaking your spinal cord is several hundred times likelier, at the very least, when attempting such a feat with skying - compared to some simple pocking and proding in an unfamiliar programming language. In the latter case, you might end up learning quite a bit, too.
Nothing wrong with starting by diving heads-in into a problem you're both well acquainted with, and genuinely curious about. Bonus points, if it produces something tangible you could use yourself.
If you know the graph theory like the back of your hand, go right ahead. If you really don't, though, you're setting yourself up for a double/triple/10x+ hurdle from the get go - for no reason at all.
If you would kindly indulge my terrible habit of going "meta" on questions of this kind: why are you learning it, in the first place? What's the end goal, if any? How certain are you that you want/need to learn, grasp, and understand "Rust the Programming Language" - and not "lower level, manually-managed memory-wise, bits-and-bolts of what makes a solid, correct, type-safe, adequately performant, piece of code"? Because that's what Rust (or, to a lesser extent, C/C++) is about.
Are you crystal clear on the difference between an "owned" and "referenced" value? Do you understand the logic behind the "memory ownership", to begin with? Are you familiar with different kinds of interpreters/compilers? Strong and weak type systems, and what they're capable of?
If not - "learning Rust" is a terrible goal, from the get-go. Try "learning systems programming", instead, "with Rust's compiler as a guide, to avoid messing with things I shouldn't mess with".
Read the Rust book. Watch some C/C++ tutorials. Try to wrap your head around category theory. Might make things easier if you ever have to deal with co/in/contra-variants, later on. Tell your "guide" you really know best, once in a while, and read into some &mut u8 you've just placed on the stack as if it was a *mut [u8; 1024] before overwriting the heck out of it with zero's, to see what all of that "UB is bad" thing is all about. Get a sense of what can be done, before you get bogged down in the details of why "Rust the programming language" wants it done its way.
Just make sure to build on what you're learning gradually, step-by-step. Don't mess with traits, generic over three lifetimes and five different types, each bound by its own set of constraints - until you've got a solid sense of what a trait even is. Read the book, port a project, experiment a lil.
See "errors" as less than something that's being "thrown" at you, and more of "you probably don't understand what you're doing here, do you?" Then measure the amount of learning you still have to do by the amount of "yeah, that's true" answers you come back with - instead of "oh, good catch".
Depends on your experience. I used the book but I didn't like that it introduced "something" and didn't explain that "something". I also looked at the other docs. I decided to just start writing code.
I like the idea of Rust for C programmers (someone mentioned that book), or Rust for Python, etc.
My impediment is using a browser to access the docs (i posted a Q about that here).
Feel free to create create issues at GitHub - StefanSalewski/Rust_for_C-Programmers: Selected chapters of the book as Markdown sources, I still try to improve the book. I tried really hard to avoid using things before explaining them, as I hate that myself. But as Rust is such a complex language, this is not always possible. And I tried to avoid repetitions and verbosity. With the repetitions, that is also hard -- pattern matching and traits are needed at a lot places for example, so I was not able to avoid some repetitions. I will try to reduce repetitions further, but actually I think that repeating some content that was discussed already in an earlier chapter does not hurt that much.
I am not a Rust programmer, so I haven't read much of the site or purchased the book, but I think it would have really helped to find this post earlier in my learnings, I think this blog might be a valuable resource site, and I expect the book could be of value:
Sorry, but why do you think that? From the table of content, and the free chapter, it seems to be a short collection of mostly trivial examples. In no way a replacement of a real Rust text book. The fake 5 star reviews should not surprise us in these days, but the way how the name of Jon Gjengset is misused for advertising is "interesting".
Absolutely not a replacement, but I see the Rust bible as more of a reference. People are struggling to learn Rust. I've been programming for 40 years. I've used Rust for years and still don't understand it, which doesn't help to grow the community. If it's your first language, then maybe the book is the best place to start. If you're coming from something else (which is probably the audience that Rust needs to target), it's quite a struggle, just the basics like strings are so frustrating that you want to run away screaming. I didn't check the book closely or buy it; I was hopeful. Quite likely I was wrong.
Obviously not the official book, as I see that one more as an extended tutorial, explaning the most basics concepts.
Rust is a complex language. It is not possible to teach the basics (without macros and async) in less than 500 pages -- I have tried that :-). Short, tutorial like papers and project based introductions might be useful for some. Perhaps when someone is still unsure if they want to actually learn Rust. But well, Rust is a well known language, so it should be clear if someone want to learn it, so why waste time with such trivial mini books which often are not even freely available online?
[EDIT]
I've been programming for 40 years.
I have actually some problems really understanding your texts, I had to read both of your posts twice, and still are unsure about some sentences. My personal advice: If you have some programming experience and wants to really learn Rust, read my book. It might be not perfect, but it is close to, and I will still try to improve it a bit. I just spent a halve week rewriting chapter 13 about iterators for the third time
Yes, that book; I think that's where most people start, but never find time to complete. Developers are often put on projects without time for 10 pages, let alone 500, and younger developers seem to have less attention span anyway. Not all of us are in school with time to research thoroughly before coding; we turn to StackOverflow and LLM. This is reality, and it's worsened by LLM that can generate plausible code that the developer cannot even follow.
This situation reduces Rust adoption and quality and prevents the C/C++ migration that we absolutely need, in fact increasing the divide. People fear change, especially older people. I think everyone in the Rust community must know that many developers are inappropriately biased against Rust, and the learning curve is one of the reasons. The Rust community honestly appears arrogant and offensive to the uninitiated sometimes - like "we're better than C/C++ but you're not smart enough."
So even for me, going back to the original question...what is the best path to learn Rust? Does this thread already cover it? This may be the most significant issue with Rust (with crate/toolchain vulnerability also being a primary concern).
I was just trying to add one resource I found valuable; sorry if that was offensive.
I just told you, see my edit above. But I agree, Rust is complex and not that easy. I have to admit that I have problems remembering all the details, and for most questions here in the forum I have no answers, often I not even understand the questions
[Edit]
The Rust community honestly appears arrogant and offensive to the uninitiated sometimes - like "we're better than C/C++ but you're not smart enough."
In this forum? Absolutely not. At Reddit? Some people perhaps -- I rarely look at Reddit. At Reddit there are always a few strange individuals pretending to be a programmer (or book authors, or whatever) telling strange things -- I just ignore them. On IRC, YouTube, Discord? No idea, I do not use that. Arrogant and unfriendly people can be a plague, even more when they regard themselves as "core devs". That is the main reason why I left Nim two years ago. Others left as well for same reasons.
Agreed! I absolutely love the language (when I can figure it out) and most of the Rust community, but it's easy to misinterpret things, especially when you're frustrated with the language and ask a newbie question on the wrong channel. And even here, sometimes you just don't get any useful response at all, which looks bad, like the community is not there. And when someone suggests that you read 500 pages before proceeding. And to some extent, any developer community faces similar challenges, but they somehow seem more pronounced with Rust.
I also appreciate the work that you have done at no cost to the community. I am in the same vein. At the same time, I don't have time to read it, so for me this thread is closed.
Pretty much everything you have said there is true of C++ and others. Heck, Javascript books are generally huge. Having used so many languages in my career I don't see Rust as particularly worse in that respect. I managed to create useful code in Rust that went into production about as quickly as I did with JS when I first had to use that some years ago. That's not to say I had learned all of Rust or even the best way to use all its features.
Indeed people fear change. But lest's not be agist. I'm old enough to have retired years ago but was overjoyed to find Rust at just the time we were launching our little start up. I really did not want to continue with C++ after a couple decades of it annoying me.
I see that claim made around the net quite a lot when the subject of Rust turns up. But I have never heard a Rust developer or user actually saying such a thing. Where have you seen it?
I'm honestly trying to add value here, not to be offensive, troll, or rile the community, because I actually know quite a bit about building developer community (see links in my profile here).
Certainly not my intention, but my perspective based on experience. Again, I've been coding for 40+ years, starting with a C64.
And herein may lie one of the problems. If Rust is only attempting to appeal to C/C++ programmers, it will not attract C# developers, JavaScript developers, and others. This creates at least the appearance of an exclusive and elitist community that slows growth.
That would be true. But I don't think Rust is only attempting to appeal to C/C++ programmers. It is perfectly possible to write code that would be comprehensible to those with C#, JavaScript etc experience and I don't believe getting them up to speed enough to be able to write such code can be so hard. Most of the code I have produced is that simple. I don't write macros and if I feel the need for a lifetime tick mark I take that as a hint I'm going down the wrong road. I do that deliberately so as not to frighten away my colleagues that mostly Python and JS heads.
"Slashdot". OK. I see the problem. As great and useful as Slashdot was back in the day it is now a swamp.