Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Rust strings to UTF-16?

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);
    }
}
like image 620
Gigih Aji Ibrahim Avatar asked Aug 08 '14 06:08

Gigih Aji Ibrahim


People also ask

Are Rust strings UTF-8?

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.

How do you parse strings in Rust?

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().

Are strings mutable in Rust?

Rust owned String type, the string itself lives on the heap and therefore is mutable and can alter its size and contents.

What is a Rust string?

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.


1 Answers

Rust 1.46+

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);
}
like image 191
ChrisD Avatar answered Sep 22 '22 00:09

ChrisD