(我是 Rust 初学者)我有三个文件:main.rs、board.rs 和 case.rs。我想将case.rs包含在board.rs中,board.rs包含在main.rs中,这样board就使用case,我们可以在main中访问board。
我已经成功地将电路板添加到主要部分,但我所做的方式似乎不适用于第二部分。
我试图将每个文件的内容封装到“mod {}”中,但它并没有改变问题。我还尝试了“mod”和“use”的每种组合。
每个文件都在文件夹 src/ 中,如果可能,我希望它们不要从那里移动。
主文件
mod board;
fn main() {
let mut b: Board = Board::new();
}
Run Code Online (Sandbox Code Playgroud)
board.rs
mod case;
pub struct Board {
board: [ Case; 9 ]
}
// There is also the impl part of course, let's keep it short
Run Code Online (Sandbox Code Playgroud)
案例.rs
pub enum Case { Empty, Full(Player) }
Run Code Online (Sandbox Code Playgroud)
将 VSCode 与 Rust 插件一起使用,board.rs 文件第一行的“case”字样带有红色下划线,它表示:
“找不到模块case
帮助的src/case.rs 文件:将文件命名为 board\case.rs 或 board\case\mod.rs 目录中的“src””
为什么不在当前目录中搜索?
我的目标是将 Rust 程序编译为尽可能小的二进制文件并提取机器代码。我编写了一个非常简单的程序来测试。
.cargo/配置
[target.x86_64-pc-windows-gnu]
rustflags = ["-C", "link-args=-e _start -static -nostartfiles"]
Run Code Online (Sandbox Code Playgroud)
Cargo.toml
[package]
name = "r"
version = "0.1.0"
edition = "2021"
[profile.release]
panic = "abort"
opt-level = "z"
lto = true
codegen-units = 1
Run Code Online (Sandbox Code Playgroud)
主程序.rs
#![no_std]
#![no_main]
#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
loop {}
}
#[no_mangle]
unsafe fn _start() -> isize {
42
}
Run Code Online (Sandbox Code Playgroud)
我编译cargo build --target x86_64-pc-windows-gnu --release并提取该.text部分objcopy -j .text -O binary target/x86_64-pc-windows-gnu/release/r.exe r.bin,但是当我显示机器代码时,我得到的比我预期的要多:
% objdump -D -b binary …Run Code Online (Sandbox Code Playgroud)