How to get the data in a file from an crate

I created a lib type project. There is a "resource" folder under the src folder in the project, and the "resource" folder stores a "test.dat" data source file. The lib contains the code to read the data source file (the file path address is "./src/resources/test.dat"), and provides an external access method for this logic. Then I uploaded this lib release to When I create a new binary project "A", this "A" project depends on the lib through Cargo.toml. But I found that when I try to call the external access method provided by the "lib" in the "A" project, an error that the file cannot be found will be reported inside the lib. How can I deal with this problem please?

If you want to store data in your library, you'll have to embed it in the source somehow. Cargo doesn't copy over arbitrary files – besides, even if it did, how would you know its path when your library is used in another project?

If your data file isn't too big, you could try using include_bytes!() (for binary data) or include_str!() (for text).


I switched from the Java language to the Rust language. The jar package of the Java program can pack the data source file in, and can find the corresponding data source file according to the relative path. Why can't the Rust language do this? Is it because the Rust language has no runtime?

The closest equivalent in any language that compiles to native executables would be bundling the data into the binary, which you can do in Rust with above built-in macros.

1 Like

If you want something like Java's resource system, where you have many file-like items accessed by paths that were packed into the .jar (here, the executable), then you can use the include_dir library to accomplish that.

But if you don't have a specific need to collect all files in a directory, using include_bytes!() or include_str!() is the simple and conventional thing to do.


Sorry, I don't quite understand what you mean. Do you have relevant information for reference? After all, what I expect is that I develop a public lib (the lib contains the data source files that need to be processed, contains the read and write operations of the data source, and the corresponding processing logic), and other projects only need to rely on this lib, And get the corresponding result through the open interface I provided. Thus, there is no need to pay attention to the data source file itself.

I think I see what you mean. I'll go and have a look at this related doc first, and try it out. thank you very much.

You would write this in the source code of your library:

static TEST_DATA: &[u8] = include_bytes!("test.dat");

and the Rust compiler takes care of ensuring that the bytes will be available with your library, wherever that library is used.

Thanks a lot, I'm trying to do this.

That's exactly what I suggested above.

1 Like

Thank you very much, the problem has been solved according to the solution you provided.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.