How to get UTF-8 index of char in Rust

I'm very new to rust. How I can to get UTF-8 index of char symbol in Rust.

Here you have a reference to utf table.

let bracket = '[';

fn get_utf(c:&[u8])->&str{
  // don't know how to obtain utf index
}

let result = get_utf(bracket); // 005B

I tried this function, but it does not work in a way I expect.

This crate might be useful, but I don't know how to use it.

Sorry, not much of my effort.

Upvotes: 1

Views: 2716

Answers (1)

kmdreko
kmdreko

Reputation: 60527

A char in Rust represents a Unicode scalar value. You can use as to cast it to a u32:

let bracket = '[';
let result = bracket as u32;

println!("{:04X}", result); // prints "005B"

See also: How to get a char's unicode value?

Upvotes: 5

Related Questions