Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Basic questions/confusion about "use" and "self"
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 use
s 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:
self::
exist on its own as an import statement, or does it require some kind of crate or module scope declared before it?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
๐ท๏ธ rust_feed