How to climb the cliff!

I have written thousands of lines of code, in many languages from assembler to smalltalk. And I have delved deeply into the rust "Book", and researched rusup doc, but I face a cliff. I had hoped to find a syntax definition ((Backus - Naur would be nice) and a library / macro document with definitions divided into sections like "String handling", "Text manipulation", "File handling" etc.. But I don't find these. Without them I do't see how to progress beyond stringing together bits of code from the examples (with no understanding of why they work.) Am I looking in the wrong place, or in the wrong way?
BTW all the stuff in the "Book" about why Rust is safe is very important - but for me at least totally irrelevant, indeed intrusive, when trying to get some steps forward into the language.

1 Like

You can find something like that throughout the reference (click on relevant chapters on syntactical things like items, patterns, or expressions). Note that the reference may look similar to, but is a different book than “the book”.

1 Like

Thanks, for the very swift response.
I looked - it left me bafled. For example where do I find an explananation of
let contents = fs::read_to_string(file_path).expect("main.rs not found");
Why "::", are there other options to ".expect". How could I then find the size of "Contents"? Is "Contents" a vector accessible by "Contents[index]" or somesuch, or is it something else?

My issue is not to get specific answers to these questions on this forum, but I would like to get help in understanding how to navigate the documentation to find the answers for myself.

Hmm… for libraries, usually the relevant library docs are the way to learn them in detail. For the standard library that’s the standard library documentation, but I would be surprised if you weren’t already aware of that one. E.g. module-level docs for std::string or std::fs can be entry points into more topic-specific sections.

For people with interest in less “explanatory” approaches into Rust, working with various example-focused approaches could be useful in principle. Like Rust by example or rustlings or the cookbook.

Some exposition to important concept can also be found on various third-party sources. Reading something like “Learn Rust With Entirely Too Many Linked Lists” can be fun. I don’t recall off the top of my head, but somewhere there was also a list of articles … no wait, found it! :wink:

There’s certainly also no shortage of alternative books about Rust, including paid options, which might be higher quality even in some regards, but in my experience it’s not really necessary to buy any books.

Also the compiler itself can be a good learning tool, often it makes good hints or explains problems well, and even links to more information. This of course only works sometimes, sometimes hints can be off, or you don’t even know how to approach something to begin with, but “read the error messages” is a good recommendation during learning Rust, which includes the tip “set up your compilation so that you even see the whole error messages (as output e.g. by cargo check or cargo run in the command line) to begin with”.

9 Likes

Well… the book is designed more for sequential reading of the whole thing. Eventually you’d come across this chapter and then hopefully understand that fs::read_to_string refers to an item read_to_string in a module fs.

The reference however is a place where explanations are not entirely beginner-level, but it’s designed to look things up. So you could, in principle, find all the things involved like the

  • let statement
  • having an expression after the = sign, which in this case, well… the reference is not a parser, so that’s not straightforward to find,
  • but in principle the expression then would be a method call expression between with fs::read_to_string(file_path) on the left, and expect a method with argument "main.rs not found", and so on, where then
  • fs::read_to_string(file_path) is a call expression, and
  • fs::read_to_string is a path expression and a path with two segments…

Anyways…

For understanding on a non-syntactical level, what is available you need to consider the types again.

Set up rust analyzer to gain the ability to just select anything and instantly learn its type in your editor; or write . for a method call and get a list of available methods (though sometimes that list can be quite long).

But even manually the standard library docs has great search, so you’d go search for it in the standard library like so

(click any screenshot to go to the website displayed)
Screenshot_20231205_005318

and then find the type signature there

Screenshot_20231205_005349

which tells you, Result<String> is relevant for what you can do with the result… you can even click on Result to get directly to the relevant page

Screenshot_20231205_005411

where you can find the expect method being listed.

Allow me to leave a quick side note….

Note that this also links a definition as Result<T, Error>. The documentation is unfortunately not very good at visually distinguishing things with the same name in different modules. The true “definition” is

std::io::Result<T> = std::result::Result<T, std::io::Error>

and when Result is discussed, the base type in std::result, with two type arguments, is usually meant, not any of the type synonyms with a fixed error type.

Back to the expect method. As we can see from its signature, this method returns the type T (for Result<T>) which means…

Screenshot_20231205_010157

… that in out case for Result<String>, we have that contents is of type String

Screenshot_20231205_010307

(click…)

Screenshot_20231205_010418

where once again, the side-bar is a useful tool to not get lost among the numerous methods. If you scroll down, you could e.g. find len method for getting the “size” of contents.

Screenshot_20231205_010626

Another sometimes useful tool is the button to collapse stuff

Screenshot_20231205_010730

which gives a better overview over many methods

24 Likes

steffahn,
Very much thanks. Some light is dawning through this interaction which I did get from the direct documentation. You have also given me a host of places to investigate - which I will follow though. But this will take me a while, so I will close my end for now.
Thanks again.

2 Likes

Hi petermat,

I am feeling the same at the moment. Some of the chapters in The Book, I have already read three (3) times.

-- With Rust, I do feel hitting a wall sometimes.

Understanding the documentations can indeed be problematic, too. I feel that the authors just don't have enough energy to write detail explanation -- which I can appreciate. But it makes learning for beginners a bit challenging still.

We all learn differently. For me, I ask myself, what do I want to know, then I set out finding material for that -- and surely enough, soon enough, I run into a wall.

But a little patient, I think we will be okay.

But I do appreciate the feeling, after spending hours of searching and reading, it seems that we are back to a square one... New structs, new methods etc pop up all the times and their documentations don't seem to be beginner-friendly.

For me, personal, reread and made my own summary of The Book's Chapter 10 -- Generic Types, Traits, and Lifetimes help to read documentation a bit easier.

Also some very helpful person in this forum posted this link before Tour of Rust's Standard Library Traits -- which helps me enourmously. The author of this article also has other very informative posts.

I understand that my reply is not related to the specific problem that you have outlined, but for me, after what I've done the above, I found that my understanding of Rust has progressed a little. Still hitting walls, but it's a progress, regardless.

Thank you for your post, it reflects my frustrations with Rust precisely.

Best regards,

...behai.

1 Like

Given that you claim to be an experienced programmer, I find this expectation surprising. Almost no mainstream language has a perfectly formal definition of its syntax, and it's not a problem. Syntax is mostly arbitrary, boring, and the issues that it may present (mainly various kinds of ambiguity) have already been solved for you by the writers of the compiler. You also can't learn a language by staring at its BNF grammar.

https://std.rs is a shortcut to the documentation of the standard library. It has an overview at the front page from which you can navigate to the most useful sections.

Again, surprised to see this given your claim of knowing many languages. You make progress with Rust in exactly the same manner as you would with other languages. You write your own code and get feedback from experienced users. (Better yet, you read experienced users' code yourself, which allows for a much faster feedback loop.)

5 Likes

For a first glance look at a new (to me) language and it's syntax, I check if there's a learnxinyminutes for it.

Here's Rust's: Learn Rust in Y Minutes

It's not an exhaustive reference, but gives some idea how things look and work. Rust is a difficult language to write this for because it has so much syntax!

No, not really. The syntax is nothing. Rust's syntax is very regular and familiar to most programmers (except maybe those who have only ever worked with whitespace-sensitive languages such as Python and Haskell).

People who struggle with Rust usually do so because it makes them think about problems they didn't even know existed (as other languages paper over such details with worrying levels of forgiveness).

2 Likes

I wouldn't call that nothing. But I don't want to fight you about this

Well, you have to have a separate production for every single construct. But most of these are found in many other languages in almost exactly the same form. Out of these 39 variants:

  • 1 is not really a thing ("Verbatim", just reserved for unstable stuff that syn doesn't yet know how to parse)
  • 7 are specific to Rust (macro, match, repeat, unsafe, yield – not even stable –, closure, try, try block)
  • and the rest are trivial things that you can find in almost every other mainstream language (binary infix and unary operators, loops, if-then-else, arrays, async, literals, tuples, method calls).

The cognitive complexity of syntax doesn't depend on the number of constructs that exist per se. It depends on the familiarity and similarity with previous experience, and the regularity of the ways these constructs fit together. Nobody is going to be surprised about +, -, *, /, parentheses, or array indexing. Look, these are 6 separate things already! (Not really, probably – the human mind compartmentalizes and compresses heavily to be able to perform content-addressing; all of these except array indexing probably fall into the "oh yeah math formula yadda yadda" drawer in the mind of most people.)

In general, "number of things" is a terrible, terrible measure of complexity in any half-serious context. Brainfuck only has 8 characters as its whole syntax, yet I wouldn't ever characterize it as "4 times easier to read" than Rust.

5 Likes

It maybe surprising but: I too have used many languages over many years, from BASIC to Javascript, from assembler to C/C++, from Ada to Rust. Passing through ALGOL, LUCOL, Pascal, etc along the way, even Verilog, I too had trouble getting to grips with Rust and its documentation.

It was not the lack of a formal syntax, BNF, or whatever. I cannot really put my finger on where the difficulty was for me. Especially now that I have been around the block a few times and got the lie of the land (as it were).

What did cause my mind to stall was, and still is, a kind of meta-problem. It's the English language I see used when people describe Rust or particular problems people have. It is full of words and terms that were/are totally alien to me (Having never had a CS education), things like "closure", "trait", "product/sum type, "covariant", "future".... And that's before we get to all the FP lingo like "monad".

Slowly slowly I'm getting the hang of it.

4 Likes

But this is exactly what I am saying, isn't it?

2 Likes

Yes, yes. I was agreeing. I don't think I have seen a BNF for a language since Pascal.

After reading the official book, you may consult additional resources like

Additional, there are many more books available. I started learning Rust three months ago, and for me it is still some way to go. Rust is a large, not too simple language, with a large library and many external packages. So it is expected that learning will take some time. But even learning Ruby or Nim took some time. Both languages are simpler than Rust, but had not so many free good learning resources. Well, for the old and small languages like Pascal, C, Oberon, Modula, we had a concrete syntax specification, but what made them easier to learn for me was more their small core and tiny library size.

[EDIT]

Sorry, I might have forget the most important point: I am sometimes using AI tools like GPT-4 now. GPT-4 is for learning like having a good smart friend sitting next to you. The Rust questions are not always answered perfectly, but often we get fast and good advice. It can answer questions, suggest code, convert code from other languages. And most importantly for me as a non native English speaker, it can make suggestions for a more fluent and grammatically correct text.

[EDIT 2]

I just wondered what is the equivalent of Nim's CountTable. Asked GPT-4, and got an example which is working in Rust playground:

https://chat.openai.com/share/a558a06c-921f-4b02-82d1-7eb2c1c11bca

3 Likes

It sounds like you've studied diligently! When I first tried and failed to learn Rust I tried to run into it head first and figure it out as I went along and I ran into a wall. Only after I studied it did I know what I was doing enough for the "bash my head into it until I understand" to work. But it sounds like you may be making the opposite mistake, expecting to gain a rich understanding from just reading prose! I recommend you keep bashing your head into it trying things and googling and moving on to something else if you can't figure out the one thing right now. It'll all come together with experience.

1 Like

Hi ZiCog,

My language resume is not as impressive as yours, but I too, and we all, have learned scripting and languages on our own during our working life, I have learned C, C++, Cobol, Assemler, Pascal in uni. Working commercially mostly with Pascal/Delphi as backend. I have learned Visual Basic, ASP, JavaScript, Java ( forgot all now ), PHP, Python, NodeJs on my own. Working commercially with Visual Basic, ASP and JavaScript, PHP.

I've been learning Rust since August, 2023. I can do basic "web application" ( ha ha ) now. Know how to talk to databases, I have even written an entire CLI to calculate resistor value based on input colour codes fully with tests in Rust. And yet, I still find myself lost.

I still have not been able to articulate what it is that I find difficult in Rust.

Best regards,

...behai.

1 Like

May I suggest

1 Like

Sounds great. I see Advent of Code everywhere, seems to have grabbed peoples attention. I wonder who has the time for all that? I can't keep up with solving all the problems I'm supposed to be working on or all the side projects I'd like to get done or all the other interesting rabbit holes I'd like to go down. And that is in just the programming things I like to do. And conveniently we have a delivery scheduled for before Christmas.

Good luck and Merry Christmas everyone!

5 Likes