Why does include_str! act differently on different OSs

Windows Rust compiler:

// This compiles fine.
include_str!("someDir\\file.txt")

If I copy the code to a Mac or Linux compiler:

// This does NOT compile.
include_str!("someDir\\file.txt");

// This compiles.

include_str!("someDir/file.txt")

I know that the way paths are handled are slightly different from Windows to Linux based systems, but why doesn't include_str! account for this?

If this is not going to be fixed, is there anyway to get around this?

As I understand it, the forward slash form should work for all OSs. Windows accepts the backslash, but it also accepts forward slashes. include_str!("some_dir/file.txt") is fully portable.

I don't know the exact reason, but I might speculate. On linux, at least, \\ is a fully valid part of a path. One can make a directory named "someDir\full name". If APIs like include_str! turned backslashes into forward slashes on non-Windows operating systems, then it would break directories like this.

Ideally we'd use a non-string-based path separator - like std::path::MAIN_SEPARATOR. But include_str!() needs the path at compile time before things are resolved, so it can't really use that.

Since / works as a cross-platform directory separator, maybe it isn't considered a high priority to make include_str!()'s API directory-separator-agnostic?

4 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.