我有一个“use”不起作用,即使它在我的 src 中

Jam*_*den 1 rust rust-crates rust-cargo rust-analyzer

包含所有内容的目录称为p,位于p

.git
.gitignore
Cargo.lock
Cargo.toml
todo.md
src
target
Run Code Online (Sandbox Code Playgroud)

src

action_functions.rs
execute_action_functions.rs
lib.rs
main.rs
network.r
Run Code Online (Sandbox Code Playgroud)

network.rs

pub mod network{
        use rand::Rng    //to generate random numbers
    //use crate::action_functions::{/*insert all the action functions */};
    use rand_distr::{Normal, Distribution}; //to generate different dist. of random numbers
    use serde::{Serialize, Deserialize};    //to save/load my neural network
    use std::fs::File;
    use std::io::{BufReader, BufWriter};
    use chrono::Utc;            //for timestamp
    use std::path::Path;        //for help in representing file paths

Run Code Online (Sandbox Code Playgroud)

然后我有一堆 pub 结构,然后我有impl NeuralNetwork {内部 pub mod 网络和一堆 pub 函数。

Cargo.toml

.git
.gitignore
Cargo.lock
Cargo.toml
todo.md
src
target
Run Code Online (Sandbox Code Playgroud)

main.rs

use crate::network::NeuralNetwork;
fn main() -> std::io::Result<()> {
    let mut network = NeuralNetwork {
        layers: Vec::new(),
        weights: Vec::new(),
        biases: Vec::new(),
    };
    network.initialization(10, 10, 2); // Initialize with your parameters

    // Print the network
    network.print();

    // Save the network
    network.save_v2()?;

    // Load the network
    //let path = "D:\\Downloads\\PxOmni\\rust_save_states\\your_file_name"; // Replace with your file path
    //let loaded_network = NeuralNetwork::load(path)?;

    // Print the loaded network
    //loaded_network.print();

    Ok(())

}
Run Code Online (Sandbox Code Playgroud)

lib.rs

pub mod network;
//pub mod Network;
pub mod action_functions;
pub mod execute_action_functions;
Run Code Online (Sandbox Code Playgroud)

action_functions.rs除了: 之外的所有代码都被注释掉了,并且pub mod action_functions {} 中的所有代码都被注释掉了action_functions,对于 也是如此execute_action_functions。为了透明起见,这是我的execute_action_functions.rspub mod execute_action_functions {}

并且 rust-analzer 没有在任何地方检测到错误,也没有编译错误。除了这个:

error[E0432]: unresolved import `crate::network`
 --> src/main.rs:5:12
  |
5 | use crate::network::NeuralNetwork;
  |            ^^^^^^^
  |            |
  |            unresolved import
  |            help: a similar path exists: `p::network`
Run Code Online (Sandbox Code Playgroud)

每当我这样做时,主要是: use crate::p::network::NeuralNetwork;

它说:

error[E0433]: failed to resolve: could not find `p` in the crate root
 --> src\main.rs:5:12
  |
5 | use crate::p::network::NeuralNetwork;
  |            ^ could not find `p` in the crate root

For more information about this error, try `rustc --explain E0433`.
error: could not compile `p` (bin "p") due to previous error
Run Code Online (Sandbox Code Playgroud)

然后我尝试添加一个[[bin]]并执行cargo run --bin your_binary_name相同的错误:

action_functions.rs
execute_action_functions.rs
lib.rs
main.rs
network.r
Run Code Online (Sandbox Code Playgroud)

我什至尝试添加:

pub mod network{
        use rand::Rng    //to generate random numbers
    //use crate::action_functions::{/*insert all the action functions */};
    use rand_distr::{Normal, Distribution}; //to generate different dist. of random numbers
    use serde::{Serialize, Deserialize};    //to save/load my neural network
    use std::fs::File;
    use std::io::{BufReader, BufWriter};
    use chrono::Utc;            //for timestamp
    use std::path::Path;        //for help in representing file paths

Run Code Online (Sandbox Code Playgroud)

并且没有变化。

Cha*_*man 5

你这里有两个错误。

第一个是两个不同的板条lib.rsmain.rs。如果你想lib.rs从访问main.rs,你不能用 这样做,crate因为它不是同一个箱子;您必须使用板条箱名称 ( p)。

第二个是,pub mod network;在 lib.rs 中已经创建了一个 module network,如果您pub mod network { ... }在 network.rs 中创建了一个 module ,那么您将创建一个嵌套模块:p::network::network