Editor's note: This code example is from a version of Rust prior to 1.0 and is not valid Rust 1.0 code, but the answers still contain valuable information.
I want to pass a string literal to a Windows API. Many Windows functions use UTF-16 as the string encoding while Rust's native strings are UTF-8.
I know Rust has utf16_units() to produce a UTF-16 character iterator, but I don't know how to use that function to produce a UTF-16 string with zero as last character.
I'm producing the UTF-16 string like this, but I am sure there is a better method to produce it:
extern "system" {
pub fn MessageBoxW(hWnd: int, lpText: *const u16, lpCaption: *const u16, uType: uint) -> int;
}
pub fn main() {
let s1 = [
'H' as u16, 'e' as u16, 'l' as u16, 'l' as u16, 'o' as u16, 0 as u16,
];
unsafe {
MessageBoxW(0, s1.as_ptr(), 0 as *const u16, 0);
}
}
When Rustaceans refer to “strings” in Rust, they might be referring to either the String or the string slice &str types, not just one of those types. Although this section is largely about String , both types are used heavily in Rust's standard library, and both String and string slices are UTF-8 encoded.
To convert a string to an integer in Rust, use parse() function. The parse function needs to know what type, which can be specified on the left-side of assignment like so: let str = "123"; let num: i32 = str. parse().
Rust owned String type, the string itself lives on the heap and therefore is mutable and can alter its size and contents.
The Rust string literal is a string slice that always references a sequence of UTF-8 characters. We mainly use it when we know the value of the string at compile time. It can be used as a view into a string object. String literals are static by default, meaning they do not mutate.
For static UTF-16 strings, the utf16_lit
crate provides an easy to use macro to do this at compile time:
use utf16_lit::utf16_null;
fn main() {
let s = &utf16_null!("Hello");
println!("{:?}", s);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With