简体   繁体   English

无法从二进制文件访问 lib.rs 文件

[英]Unable to access lib.rs file from binary

I am unable to access a library from my binary.我无法从我的二进制文件访问库。

Here is what my cargo.toml looks like这是我的 cargo.toml 的样子

[package]
name = "app"
version = "0.1.0"
edition = "2021"

[dependencies]
<--snip-->
[lib]
path = "src/lib.rs"
[[bin]]
path = "src/main.rs"
name = "realio"

the application root应用程序根

.
├── Cargo.toml
├── src
│   ├── lib.rs
│   └── main.rs
└── test
    └── integration.rs`

and my main.rs和我的 main.rs

#![crate_name = "realio"]

use env_logger::Env;
use realio::run;
use std::net::TcpListener;

#[tokio::main]
async fn main() -> std::io::Result<()> {
    env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
    let listener = TcpListener::bind("127.0.0.1:8000").expect("failed to bind port");
    run(listener)?.await
}

However, I get the following error但是,我收到以下错误

error[E0432]: unresolved import `realio`
 --> app/src/main.rs:4:5
  |
4 | use realio::run;
  |     ^^^^^^^^^^^^^^^^^^^^^ use of undeclared crate or module `realio`

I would appreciate pointers on this我会很感激这方面的指点

You're missing the name for the lib.您缺少库的名称。

This would be right:这是正确的:

[dependencies]
[lib]
name = "realio"
path = "src/lib.rs"
[[bin]]
name = "realio"
path = "src/main.rs"

But you don't need to manually declare it there if you stick with the main.rs and lib.rs naming convention.但是,如果您坚持main.rslib.rs命名约定,则无需在此处手动声明它。 Also keep in mind to change the Package name (line 2 in your Cargo.toml) to "realio", so your code still works.还要记住将 Package 名称(Cargo.toml 中的第 2 行)更改为“realio”,这样您的代码仍然有效。

You can find more infos for that in the Cargo Book: https://doc.rust-lang.org/cargo/guide/project-layout.html您可以在货运手册中找到更多信息: https://doc.rust-lang.org/cargo/guide/project-layout.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM