It is not as simple as it seems. There is one piece of information that you did not provide: in what encoding does function C expect a path?
On Linux, paths are simply arrays of bytes (0 is invalid), and applications usually don't try to decode them. (However, they may have to decode them using a specific encoding, for example, to display them to the user, in which case they will usually try to decode them according to the current locale, which will often use UTF-8 encoding.)
On Windows, this is more complicated because there are variants of API functions that use the "ANSI" code page, and variants that use "Unicode" (UTF-16). In addition, Windows does not support the installation of UTF-8 as the "ANSI" code page. This means that if the library does not specifically expect UTF-8 and does not convert the path to its own encoding, passing it in the UTF-8 encoded path is definitely incorrect (although it might seem that it works for strings containing only ASCII characters).
(I do not know about other platforms, but this is already dirty enough.)
In Rust Path , it's just a shell for OsStr . OsStr uses a platform-specific view that turns out to be UTF-8 compatible when the string is indeed valid UTF-8, but non-UTF-8 strings use undefined encoding (on Windows it actually uses WTF-8 , but it is not negotiable. on Linux, it's just an array of bytes as is).
Before passing the path to the C function, you must determine in which encoding it expects the string, and if it does not match the Rust encoding, you will have to convert it before packing into a CString . Rust does not allow you to convert Path or OsStr to anything other than str regardless of platform. For Unix-based purposes, OsStrExt feature of OsStrExt that provides access to OsStr as a fragment of bytes.
Rust is used to provide the to_cstring method on OsStr , but it never stabilizes, and it was deprecated in Rust 1.6.0, as it turned out that the behavior was inappropriate for Windows (the UTF-8 encoded path returned to this, but for the Windows API, not I support it!).