ファイル分割
src/lib.rs
- cargoのsrc下の
lib.rsは関数などを書けば、main.rsから使用できる。
// lib.rs
pub fn hello() {
println!("Hello from lib!");
}
// main.rs
use クレート名::hello; // use crate::hello;は不可だった。
fn main() {
hello();
}
src/dir/mod.rs
// main.rs
mod dir;
use crate::dir::my_function;
fn main() {
my_function();
}
pub fn my_function () {
println!("my_function");
}
Environment
$ cargo -V
cargo 1.92.0 (Homebrew)
$ rustc -V
rustc 1.92.0 (ded5c06cf 2025-12-08) (Homebrew)
$