Basic questions/confusion about "use" and "self"

โš“ rust    ๐Ÿ“… 2025-06-10    ๐Ÿ‘ค surdeus    ๐Ÿ‘๏ธ 1      

surdeus

N00b rust programmer here with experience in other languages. I'm working through the diesel "getting started" tutorial (Getting Started with Diesel) and I ran into some confusion around use and self:: references in one of the example binaries they build. Asking here because I think it deals more with rust than with diesel specifically.

The bit of code I ran into is this (src/bin/get_posts.rs: diesel/examples/sqlite/getting_started_step_3/src/bin/get_post.rs at 2.2.x ยท diesel-rs/diesel ยท GitHub), with the intro uses for:

use self::models::Post;     // <- problem import line
use diesel::prelude::*;
use diesel_demo::*;     // <- related import line
use std::env::args;

The first thing I noticed was that my editors (both Zed and VSCode+Rust Analyzer) didn't recognize models as being under self (first line), not providing autocomplete for it, and highlighting it as a parse error.

Just to make sure that it wasn't an issue with the editor or a plugin, I tried doing a quick cargo run --bin get_posts which gave a compiler error about an unresolved import for use self::models, with help: a similar path exists: 'diesel_demo::models'.

However, if I moved the use diesel_demo::* line above the use self::models line, then things seemed to parse and run correctly.

So my questions:

  1. Can self:: exist on its own as an import statement, or does it require some kind of crate or module scope declared before it?
  2. Relatedly/broadly, does order of imports matter?
  3. Is the tutorial code incorrect in how it uses self:: for these imports? Should it really be referencing the project crate specifically (diesel_demo) since that's the entity enclosing the models and not the secondary binaries like src/bin/get_posts?

Aside: a file in the same tutorial (src/bin/publish_post.rs) right above get_posts.rs uses similar imports, and was recognized fine by my editors and by the compiler. But when I started working on get_posts.rs I ran into this issue and thus my questions.

Many thanks in advance for helping clarify these for me.

[Update: added question #3]

10 posts - 5 participants

Read full topic

๐Ÿท๏ธ rust_feed