无法在 Rust 中编译多个文件

问题描述 投票:0回答:1

我是 Rust 的初学者,希望编译一个包含多个文件的项目,结构如下:

├── src
│   ├── main.rs
│   └── strutils
│       └── get_wide_str.rs

以下是

main.rs
的内容:

use windows::core::*;
mod strutils;

fn main() -> Result<()> {
    return Ok(())
}

还有

get_wide_str.rs
的内容:

use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;
use winapi::um::winnt::LPCWSTR;

pub fn get_wide_str(src: &str) -> Vec<u16> {
    let wide_chars: Vec<u16> = OsStr::new(src)
        .encode_wide()
        .chain(std::iter::once(0))
        .collect();

    return wide_chars
}

pub fn get_wide_ptr(src: Vec<u16>) -> LPCWSTR {
    src.as_ptr()
}

当我这样做时

cargo build
我收到以下错误:

error[E0583]: file not found for module `strutils`

如何在 Rust 中干净地“分割”我的代码?

所以我尝试创建一个mod.rs,但它使代码变得丑陋且冗余,而且仍然不起作用。

rust rust-cargo
1个回答
0
投票

由于

get_wide_str.rs
嵌套在
strutils
模块中,因此
main.rs
无法访问它。因此,您需要使使用
get_wide_str
的其他模块可以访问
strutils
模块——使用
pub mod

mod.rs
目录 (
strutils
) 中创建一个
strutils/mod.rs
文件,其中包含以下内容:

pub mod get_wide_str;
© www.soinside.com 2019 - 2024. All rights reserved.