Constant based on kind of lib creation

I'm working on a path handling library and I was wondering if I could hard-code in the path separator based on what kind of file Rust saves the library to. For example if the file gets saved as libpath.so then a constant for separator should be permanently /. Likewise if it's a libpath.dll file then it would be \. It would be more performant for these to be constants rather than have the library check on every method call.

Is there a way to do this kind of thing? Maybe a macro for lib output type choosing what code to persist?

Can this be achieved with cfg attributes?

#[cfg(not(windows))]
const PATH_SEP: char = '/';

#[cfg(windows)]
const PATH_SEP: char = '\\';
1 Like

It would probably be better to use std::path::MAIN_SEPARATOR instead of defining it yourself. :slight_smile:

2 Likes

Thank you!