Import Rust.rs失败
我的Import Rust.rs失败,import,rust,Import,Rust,我的src/文件夹中有三个文件:main.rs、lib.rs和cfgtools.rs。我想导入cfgtools.rs main.rs extern crate cfgtools; use cfgtools::*; fn main() { let os = get_os(); println!("Your OS: {}",os); } pub mod cfgtools; pub fn get_os() -> &'static str { let
src/
文件夹中有三个文件:main.rs
、lib.rs
和cfgtools.rs
。我想导入cfgtools.rs
main.rs
extern crate cfgtools;
use cfgtools::*;
fn main() {
let os = get_os();
println!("Your OS: {}",os);
}
pub mod cfgtools;
pub fn get_os() -> &'static str {
let mut sys:&'static str = "unknown";
if cfg!(target_os = "windows") { sys = "windows" };
if cfg!(target_os = "macos") { sys = "macos" };
if cfg!(target_os = "ios") { sys = "ios" };
if cfg!(target_os = "linux") { sys = "linux" };
if cfg!(target_os = "android") { sys = "android" };
if cfg!(target_os = "freebsd") { sys = "freebsd" };
if cfg!(target_os = "dragonfly") { sys = "dragonfly" };
if cfg!(target_os = "bitrig") { sys = "bitrig" };
if cfg!(target_os = "openbsd") { sys = "openbsd" };
if cfg!(target_os = "netbsd") { sys = "netbsd" };
return sys;
}
lib.rs
extern crate cfgtools;
use cfgtools::*;
fn main() {
let os = get_os();
println!("Your OS: {}",os);
}
pub mod cfgtools;
pub fn get_os() -> &'static str {
let mut sys:&'static str = "unknown";
if cfg!(target_os = "windows") { sys = "windows" };
if cfg!(target_os = "macos") { sys = "macos" };
if cfg!(target_os = "ios") { sys = "ios" };
if cfg!(target_os = "linux") { sys = "linux" };
if cfg!(target_os = "android") { sys = "android" };
if cfg!(target_os = "freebsd") { sys = "freebsd" };
if cfg!(target_os = "dragonfly") { sys = "dragonfly" };
if cfg!(target_os = "bitrig") { sys = "bitrig" };
if cfg!(target_os = "openbsd") { sys = "openbsd" };
if cfg!(target_os = "netbsd") { sys = "netbsd" };
return sys;
}
cfgtools.rs
extern crate cfgtools;
use cfgtools::*;
fn main() {
let os = get_os();
println!("Your OS: {}",os);
}
pub mod cfgtools;
pub fn get_os() -> &'static str {
let mut sys:&'static str = "unknown";
if cfg!(target_os = "windows") { sys = "windows" };
if cfg!(target_os = "macos") { sys = "macos" };
if cfg!(target_os = "ios") { sys = "ios" };
if cfg!(target_os = "linux") { sys = "linux" };
if cfg!(target_os = "android") { sys = "android" };
if cfg!(target_os = "freebsd") { sys = "freebsd" };
if cfg!(target_os = "dragonfly") { sys = "dragonfly" };
if cfg!(target_os = "bitrig") { sys = "bitrig" };
if cfg!(target_os = "openbsd") { sys = "openbsd" };
if cfg!(target_os = "netbsd") { sys = "netbsd" };
return sys;
}
尽管如此,我还是得到了一个错误:
编译系统记录v0.1.0(file:///home/sessho/rust/sys_recog)
错误[E0463]:找不到“cfgtools”的板条箱`
-->main.rs:17:1
|
17 |外部板条箱cfgtools;
|^^^^^^^^^^^^^^^^^^^^^^^^^^^找不到板条箱
我不熟悉锈菌和这种进口概念。这里的问题是板条箱和模块之间存在一些混淆。所有源文件都是同一机箱中的模块。听起来好像不需要
lib.rs
,您只需要一个cfgtools
模块<代码>外部板条箱用于导入单独保存的其他库;外部板条箱也需要在Cargo.toml
中声明,以便货物能够找到它们
所以应该是这样的:
main.rs
:
// Declare the module, which will be there as cfgtools.rs in the same directory.
mod cfgtools;
// Make things in cfgtools visible. Note that wildcard imports
// aren't recommended as they can make it confusing to find where
// things actually come from.
use cfgtools::foo;
// and use it:
fn main() {
foo();
}
// Note pub to make it visible outside the module
pub fn foo() {
println!("Hello, world!");
}
和cfgtools.rs
:
// Declare the module, which will be there as cfgtools.rs in the same directory.
mod cfgtools;
// Make things in cfgtools visible. Note that wildcard imports
// aren't recommended as they can make it confusing to find where
// things actually come from.
use cfgtools::foo;
// and use it:
fn main() {
foo();
}
// Note pub to make it visible outside the module
pub fn foo() {
println!("Hello, world!");
}
我将这些文件放在
src/
的cargo init--bin之后。
创建一个新的空白板条箱,然后cargo run
打印出消息。这里的问题是板条箱和模块之间存在一些混淆。所有源文件都是同一机箱中的模块。听起来好像不需要lib.rs
,您只需要一个cfgtools
模块<代码>外部板条箱用于导入单独保存的其他库;外部板条箱也需要在Cargo.toml
中声明,以便货物能够找到它们
所以应该是这样的:
main.rs
:
// Declare the module, which will be there as cfgtools.rs in the same directory.
mod cfgtools;
// Make things in cfgtools visible. Note that wildcard imports
// aren't recommended as they can make it confusing to find where
// things actually come from.
use cfgtools::foo;
// and use it:
fn main() {
foo();
}
// Note pub to make it visible outside the module
pub fn foo() {
println!("Hello, world!");
}
和cfgtools.rs
:
// Declare the module, which will be there as cfgtools.rs in the same directory.
mod cfgtools;
// Make things in cfgtools visible. Note that wildcard imports
// aren't recommended as they can make it confusing to find where
// things actually come from.
use cfgtools::foo;
// and use it:
fn main() {
foo();
}
// Note pub to make it visible outside the module
pub fn foo() {
println!("Hello, world!");
}
我将这些文件放在
src/
的cargo init--bin之后。
创建一个新的空白板条箱,然后cargo run
打印消息。如何编译?您在终端中执行什么命令?它只是货物构建
。您可以编辑您的问题以包括您的货物。toml
然后?:)(PS:StackOverflow pro提示:如果你想通知某人,最好在评论中写上@UserName!)@LukasKalbertodt没关系,已经解决了如何编译?您在终端中执行什么命令?它只是货物构建
。您可以编辑您的问题以包括您的货物。toml
然后?:)(PS:StackOverflow pro提示:如果你想通知某人,最好在评论中写上@UserName!)@LukasKalbertodt没关系,已经解决了