The rust book, crates, non-compiling examples and discoveribility


#1

Hi there. Once again I am swearing because of discoverability issues.

I had a pleasant couple of days developing a reader of a non-standard xml file using serde. The compiler succeeded in guiding me toward solutions, though most messages were not, ahem, familiar.

But now I am stuck. I presume this is a very old and familiar problem, discussed many times before. But Google did not help me, so…

I need to make a serializer. Page https://serde.rs/impl-serializer.html# gives an example. I pasted this in and compiled, this was the result:

error[E0432]: unresolved import `error::Error`
 --> src\lib.rs:8:13
  |
8 | use error::{Error, Result};
  |             ^^^^^ Did you mean `std::error`?

BTW, as I cursor up/dn through the fragment above, I get popups and I can NOT move the cursor until I press Escape. Even when I am selecting. This is a bad idea in code-editors, worse in a forum, where there code has little to no context. This is the second bad idea I encountered, after hijacking Ctrl+F. Seems this forum has quite a mind of its own.

The compiler says “Did you mean std::Error”. Well, I don’t know what I mean… I just pasted it in. This is typical with rust examples from The Book also. They fail because of “cargo issues” (I presume).

So I change “use error::{Error, Result};” to “use std::error::{Error, Result};” (thank you https://doc.rust-lang.org/std/). Now I have more errors:

error[E0432]: unresolved import `std::error::Result`
 --> src\lib.rs:8:25
  |
8 | use std::error::{Error, Result};
  |                         ^^^^^^ no `Result` in `error`

Well, now I add “use std::result::{Result};” and get even more errors, almost all like this:

error[E0243]: wrong number of type arguments: expected 2, found 1
  --> src\lib.rs:21:35
   |
21 | pub fn to_string<T>(value: &T) -> Result<String>
   |                                   ^^^^^^^^^^^^^^ expected 2 type arguments

I guess I need an IDE, so I can see where the symbol is defined.

In short the resolution of this error is non-trivial, non-discoverable. And because it is so elementary, it is shocking that a mere Error and Result, so common in The Book need

use std::error::{Error};
use std::result::{Result};

just to get started. And then it still fails. Undoubtedly I am still doing things very wrong, but if so where is the link (to be included with every example in The Book) that explains how to resolve these issues?

This is also legal:

use std::error;
use std::result;

But results in:

error[E0412]: cannot find type `Error` in this scope
  --> src\lib.rs:39:18
   |
39 |     type Error = Error;
   |                  ^^^^^
   |
help: possible candidates are found in other modules, you can import them into scope
   |
2  | use serde::de::Error;
   |
2  | use serde::de::value::Error;
   |
2  | use serde::export::fmt::Error;
   |
2  | use serde::private::ser::Error;
   |
and 3 other candidates
help: try
   |
39 |     type Error = Self::Error;
   |                  ^^^^^^^^^^^

This makes it clear there are many more Error types, and I have no way of knowing what was intended, how to choose between them?

Anyway, this is not the first time I encounter non-compiling examples or downloads. It is a big drain on novices and resolving this is part of the essentials. Yet I have found nothing to guide me.


#2

To clarify, the example is one file from the source code of the serde_json library. It doesn’t compile standalone because it’s just one module from a larger crate (but it does compile if you check out and build the whole crate).

[Update: This is incorrect; see correction from @dtolnay below.]


#3

The section you linked to is one page of a chapter on Writing a data format. If you are writing a data format (a serializer), I would recommend that you start reading from the beginning of this chapter. The page before the one you linked to discusses error handling in full detail.

@mbrubeck the code on the website is not from serde_json.


#4

So the correct approch would be to clone all of serde_json (witout the value folder, as that seems related to the json!() macro) and to modify it to implement a new format?

I, apparently erroneously, presumed I could make a single file, on top of serde, referencing it, to use it as its backend. And that this was the intent of https://serde.rs/impl-serializer.html.

The use declaration confuses in the sense that it unfortunately doesn’t inform whether the namespace is local or in a crate. I understand why, as anything local could become global at a later date, and the current system allows that without breaking code.

Thanks.


#5

The example code has nothing to do with serde_json. It parses a similar format and they both use Serde, but that’s it.

Writing a data format covers all four of the files involved in the example. The page you linked to is one of those files.


#6

Ok, so I need to start with these 3 files (as I don’t need the deserializer). And I also need to get a better grip on handling file relationships :wink:

Thank you, that’s quite a lot simpler.


#7

This is the second bad idea I encountered, after hijacking Ctrl+F. Seems this forum has quite a mind of its own.

Eh, something has a mind of its own, but that’s Discourse. Some feature related to the way it offloads distant posts in long threads. Since it’s next to useless for locating things within a post, I tend to get around this in Chrome by opening Find from the hamburger menu.

BTW, as I cursor up/dn through the fragment above, I get popups and I can NOT move the cursor until I press Escape.

Yikes! I’ve never noticed just how aggressive the smiley widget is. (almost makes me wonder if something changed recently…)

I can move left and right though.


#8

Hi, this is a bug with the book then. Which examples exactly? Please file issues!