简体   繁体   English

有没有办法在 Rust 中导入一个模块,知道它的目录而不是它的文件名?

[英]Is there a way to import a module in Rust knowing its directory but not its filename?

I'm writing a game-playing AI in Rust.我正在用 Rust 编写一个玩游戏的 AI。 The strategy module includes a folder with standardized modules that handle specific situations.策略模块包括一个文件夹,其中包含处理特定情况的标准化模块。 I want to be able to create a new module anytime I think of a new situation that I would like handled.我希望能够在任何时候想到我想要处理的新情况时创建一个新模块。

I can accomplish this in Node.我可以在 Node.js 中完成此操作。 For example, in an MVC setup, you might have例如,在 MVC 设置中,您可能有

├── src
    ├── controllers
        ├── index.js
        └── ...
    ├── models
        ├── index.js
        └── ...
    └── views
        ├── index.js
        └── ...

with a controllers/index.js file containing带有一个包含控制器/index.js 的文件

////////////////////////////////////////////////////////////////////////////////
// Imports
////////////////////////////////////////////////////////////////////////////////

// Models
const models = require('../models');

// Modules
const fs   = require('fs');
const path = require('path');

////////////////////////////////////////////////////////////////////////////////
// Module
////////////////////////////////////////////////////////////////////////////////

const controllers = {};

const basename = path.basename(__filename);
fs.readdirSync(__dirname)
  .filter(file => {
    return (file.indexOf('.') !== 0) &&
           (file !== basename) &&
           (file.slice(-3) === '.js');
  })
  .forEach(file => {
    controllers[file.slice(0,-3)] = require(`./${file}`)(models) ;   
  });

Can I accomplish something similar in Rust?我可以在 Rust 中完成类似的事情吗?

No, there is no way to import a module at runtime;不,无法在运行时导入模块; Rust is a statically-compiled language. Rust 是一种静态编译语言。

The closest thing you could do would be to write a build script that enumerates your filesystem and generates the appropriate mod and/or use statements.您可以做的最接近的事情是编写一个构建脚本来枚举您的文件系统并生成适当的mod和/或use语句。

 require(`./${file}`)(models)

Rust also doesn't have "default exports" of imports, so this pattern cannot exist. Rust 也没有导入的“默认导出”,因此这种模式不存在。 You'd have to call a specifically-named function from the module.您必须从模块中调用特定命名的函数。

See also:也可以看看:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不重命名的情况下导入名称中带有破折号“-”的模块 - How to import a module that has a dash “-” in its name without renaming it 如何导入名称以“#”开头的模块? - How do you import a module with its name starting with '#'? Python:如果我将路径作为字符串,如何导入模块? - Python : How to import a module if I have its path as a string? Python将函数从另一个目录导入文件,文件又从其自己的目录导入另一个函数 - Python import function to a file from another directory which in turn imports another function from its own directory 为什么我只需要从模块导入子类而不从另一个实例导入它的基类和它的属性? - Why did I need only to import child class from module without importing its base class and its properties from another instance? 如果在另一个应用程序中打开了我自己的文件夹,则无法在 python 中导入我自己的模块 - Cannot import my own module in python if its folder is opened in another application 如何从URL获取模块并将其导入,从而忽略其有问题的依赖关系? - How can I get a module from a URL and import it such that its problematic dependencies are ignored? 如何在ES6中导入名称中带有连字符的angularjs模块 - how to import angularjs module which has hyphens in its name in ES6 如何使用名称而不是文件名导入python模块 - How to import a python module with a name not the filename Python:按名称加载模块 - Python: Load module by its name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM