How to automatically expose all .rs files in a directory in a module?

萝らか妹 提交于 2021-01-05 07:51:04

问题


I have module files generated from protobuf definitions. There are a lot of files and they will grow with time. This is the structure:

proto_rust/src/lib.rs
proto_rust/src/protos/{lots of auto generated .rs files}
proto_rust/src/protos/mod.rs

Since there are lots of files in proto_rust/src/protos/, it does not make sense for me to manually put them in mod.rs. Is there a way to expose all them to lib.rs? Something like pub mod *.


回答1:


Use dtolnay's automod crate.

automod::dir!("path/to/directory"); 

This macro expands to one or more mod items, one for each source file in the specified directory.

The path is given relative to the directory containing Cargo.toml.



来源:https://stackoverflow.com/questions/64111483/how-to-automatically-expose-all-rs-files-in-a-directory-in-a-module

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!