I analysed 5000 crates to find the most common standard library imports

If I'm not mistaken, spam, toy projects and name-squatting crates are already under-represented in the statistic, because they tend to include very little code, and therefore few std imports.

About outdated or abandoned projects, I don't see much of a problem here either; none of the most frequent std imports from the spreadsheet are deprecated or obsolete APIs as far as I know.

The purpose of this method was just to decide which APIs to add to the new prelude, if the next Rust edition includes a new prelude. I get the feeling that some people are overthinking this.

2 Likes

:grin: You got me!

That said, I really see this as a super useful tool, one that I personally want to see extended out further. The work that @Kestrer has done just made it obvious how useful this methodology can be.

With the current list, what conclusions could you really make? I'm not sure it tells so much. Maybe it can be used to support the inclusion of HashMap, that's it? I don't see much of a pattern otherwise. (I don't see anything that says that the current prelude is badly missing something, either - use lines are always a part of Rust.)

2 Likes

std::fmt would be the most useful addition in my opinion (and this is backed up by the survey), and yet it doesn't make very much sense to do so for several reasons. The current prelude is fine, IMO.

Perhaps though it's missing some newer traits: TryFrom and TryInto would be good candidates to add. These are down at positions 31 and 58 in the survey list, but there are several reasons not to count this against them: (1) they are relatively new and thus less known/used, (2) they augment From/Into (though without completing the conversions story), (3) as traits they cannot (conveniently) be used directly via import path.

4 Likes

I wish we had a tool that would just automatically fix imports for you: remove unused imports, and add imports for unresolved names by guessing what they're meant to refer to. That wouldn't eliminate the need for a prelude, but it would make it less punishing for something to not be in the prelude.

I guess rust-analyzer can do this – yet another reason I should try it out – but it would be easier to adopt as a standalone tool.

2 Likes

rust-analyzer doesn't add imports automatically at the moment, but IntelliJ-Rust does. In rust-analyzer when you click on an unknown name, a light bulb appears with an option to import it.

This would be nice, I thought cargo fmt would remove unused imports but it doesn't, although this is deviating a bit too much from the main topic here. Just saying some cargo tool would be nice to do it.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.