Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Operating systems use their own string encoding that they use for file system paths. This means that the Rust strings cannot be used directly with operating systems. Here are the differences between Rust and the main operating systems:

OsStr and OsString (part of the std::ffi module) allow to make the transition between Rust’s strings and the operating system:

See Table 1 and Table 2 for lists of the most useful methods for these both types.

Table 1:Some methods of OsStr

Method

Description

display()

Returns an object that represent the current string and can be safely printed, eliminating non-Unicode characters.

is_ascii()

Returns true if all characters are ASCII.

is_empty()

Returns true if the string is empty.

len()

Returns the string’s length.

new(s)

Creates a new OsStr slice from a string slice.

make_ascii_lowercase()

Puts ASCII characters in lowercase.

make_ascii_uppercase()

Puts ASCII characters in uppercase.

to_ascii_lowercase()

Returns an OsString with ASCII characters in lowercase.

to_ascii_uppercase()

Returns an OsString with ASCII characters in uppercase.

to_os_string()

Returns a copy of this string as an OsString.

to_str()

Returns a string slice of this string is a valid UTF-8 string, otherwise returns None.

to_string_lossy()

Converts this string into a UTF-8 string, replacing non-UTF-8 characters by the Unicode U+FFFD REPLACEMENT CHARACTER (�).

Table 2:Some methods of OsString

Method

Description

as_os_str()

Converts to an OsStr slice.

clear()

Removes all content.

into_boxed_os_str()

Converts into a Box<OsStr>.

into_encoded_bytes()

Converts into a Vec<u8>.

into_string()

Converts into a String or returns the original OsString through the error value.

leak()

Consumes the internal string and returns it as a &'a mut OsStr.

push(s)

Appends a string to this OsString.

"foo.txt"
"foo.txt"
Couldn't automatically determine type of variable `s`.
Please give it an explicit type.