Rust - 如何从 src 文件夹中调用函数到示例文件夹中的代码?

发布于 2025-01-16 21:36:44 字数 253 浏览 1 评论 0原文

如果我在 src/test.rs 中有这个函数,

pub fn print_something(){
    println!("Something");
}

我如何在 example/test.rs 中调用它?

我尝试过 extern cratemoduse 但这些都不起作用。

If I have this function in src/test.rs

pub fn print_something(){
    println!("Something");
}

how can I call it in the example/test.rs ?

I've tried with extern crate, mod, use but none of these worked.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东京女 2025-01-23 21:36:44

首先,您必须在 src/lib.rs 中声明 test 模块:

pub mod test;

然后,在 Examples/test.rs 中,将其用作库:

crate_name::test::print_something(); // Replace `crate_name` by the crate name

First, you have to declare the test module in src/lib.rs:

pub mod test;

Then, inside examples/test.rs, just use it as a library:

crate_name::test::print_something(); // Replace `crate_name` by the crate name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文