0

In Rust, I have the following folder structure:

├── Cargo.toml
└── src
    ├── bin
    │   └── main.rs
    └── lib.rs

This is as recommended in this answer.

This is the (relevant) contents of the Cargo.toml file:

[package]
name = "devinit"
...

And this is the contents of my lib.rs file:

pub mod testmod {
    pub fn testfunc() {
        println!("Hello world");
    }
}

When trying to use the function testfunc() in main.rs, I can refer to it by specifying its scope:

fn main() {
    devinit::testmod::testfunc();
}

This compiles and executes successfully. However, if I were to actually 'use' it, as seen below...

use devinit::testmod::testfunc;

fn main() {
    testfunc();
}

...the compilation fails. The following code fails to compile with the error 'failed to resolve: maybe a missing crate `devinit`?'

Why is this happening? Looking at the aforementioned answer's author's project (linked in previous edits of the answer), the structure seems to be the same...

I'm pretty new to Rust, so maybe I missed something?

kosude
  • 177
  • 3
  • 14
  • 1
    I'm not able to repro with the following steps: `cargo new --lib devinit; cd devinit; touch src/bin/main.rs`, then copy/paste the contents of those files. Sometimes weird stuff like this can just be unsaved files, have you ruled that out? Also what compiler version are you on? If you're on nightly, does this also happen on stable? If what you're describing is happening, this seems like a bug in rustc – cameron1024 Sep 06 '22 at 23:03
  • @cameron1024 - Weirdly, neither can I. Following those steps creates a working project. I'm compiling on stable, and `rustc --version` tells me I'm on version 1.62.1 (I hope that's the version you were asking for). All files are saved... maybe it *is* a bug, like you said? – kosude Sep 06 '22 at 23:11
  • Yep that's the right version number. Perhaps worth trying it on the latest (1.63) - `rustup update` should get you the latest version. If this behaviour consistently happens, IMO it would be a pretty major bug. Can't hurt to file an issue against https://github.com/rust-lang/rust , but I suspect there will be similar questions asked there about reproducibility. Before doing that though, I'd recommend trying to set up a brand new project identically to the failing one, and see if you can find any differences – cameron1024 Sep 06 '22 at 23:16
  • 1
    Luckily, this was a new project anyway, so it didn't hurt to just recreate it with `cargo new`. I can't see any differences in projects, but maybe I'm just blind, lol. The new project works fine at the same path as the broken one. In any case, I will file an issue if this consistently happens. – kosude Sep 06 '22 at 23:21
  • Also, sometimes it is an stale library from a previous compilation, maybe with a bogus timestamp. In those cases deleting the whole `target` directory can also help. – rodrigo Sep 07 '22 at 09:38
  • 1
    And that's why programmers use git (or another versioning system), so you can roll back/diff if something mysteriously stops working ;) – Finomnis Sep 07 '22 at 09:38
  • I reproduced your project and it's working without issues in both cases :) I am using stable-aarch64-apple-darwin with rustc 1.63.0 – محمد جعفر نعمة Sep 10 '22 at 05:19

0 Answers0