包含所有内容的目录称为
p
,在p
中是:
.git
.gitignore
Cargo.lock
Cargo.toml
todo.md
src
target
在
src
中是:
action_functions.rs
execute_action_functions.rs
lib.rs
main.rs
network.r
在
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
然后我有一堆 pub 结构,然后我在 pub mod 网络中有
impl NeuralNetwork {
和一堆 pub 函数。
在
Cargo.toml
:
[package]
name = "p"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rand = "0.8.5"
rand_distr = "0.4.3"
serde_json = "1.0.108"
serde = {version = "1.0.193", features = ["derive"]}
chrono = "0.4"
在
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(())
}
在
lib.rs
中是:
pub mod network;
//pub mod Network;
pub mod action_functions;
pub mod execute_action_functions;
所有内容都在
action_functions.rs
中被注释掉,除了:
pub mod action_functions {}
所有代码都在 action_functions
内注释,execute_action_functions
也是如此。为了透明起见,这是我的execute_action_functions.rs
:
pub 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`
每当我这样做时,主要是:
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
然后我尝试添加
[[bin]]
并执行 cargo run --bin your_binary_name
和同样的错误:
[[bin]]
name = "your_binary_name"
path = "src/main.rs"
我什至尝试添加:
[lib]
path = "src/lib.rs"
没有变化。
你这里有两个错误。
首先,
lib.rs
和main.rs
是两个不同的板条箱。如果您想从 lib.rs
访问 main.rs
,则无法使用 crate
执行此操作,因为它不是同一个 crate;你必须使用板条箱名称(p
)。
第二个是lib.rs中的
pub mod network;
已经创建了一个模块network
,如果你在network.rs中创建了pub mod network { ... }
,你会创建一个嵌套模块:p::network::network
。