Automatically declare imports

This is a little misleading, I've been messing with rust for quite some time now, and one thing I've been really not been too satisfied with is the imports.

For example, lets just say I wanted to use the std::io library and use the input function ( stdin ) instead of me going to the top of the page and typing out "use std::io::stdin" have vs code or whatever automatically import it.

If you have rust-analyzer installed in your VS Code session, then you can have an import added by either

  • using completion (each completion choice will mention the use it adds), or
  • using Quick Fix on the not-yet-imported name.
1 Like

rust-analyzer works ok-ish for this.

If you want a solution that works without an IDE, then you could create a module in your crate, like mod prelude {…} and pub use … in it everything you usually need, and then you'll only need to put use crate::prelude::* in other files (although some users consider wildcard imports inelegant and potentially problematic).

1 Like

Also note that rust analyzer works with many other editors: vim, emacs, etc, so you don't necessarily need an IDE per se.

I do miss the Eclipse/IntelliJ/Visual Studio (the real one, not Code) feature of going through your code, looking for undeclared symbols in "importable" positions (function names, etc), and looking through your dependencies for a matching import automatically (prompting on ambiguous names, leaving unlocatable names unchanged). For namespace-heavy languages, "find what I mean" is really nice when it works.

RustRover/CLion can do this, and VsCode also has an action "go to next error in file", from where you can trigger the automatic fix.

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.