Quantcast
Channel: Active questions tagged utf-8 - Stack Overflow
Viewing all articles
Browse latest Browse all 1134

How to slice a string as utf8 in rust

$
0
0

I am writing a rust toy parser, and I want to handle UTF-8 char in my string input. I knew that I need to use chars method to get UTF-8 iterator to correctly get a UTF-8 char, but I want to slice string using UTF-8 index. Is there any method I can use ? I looked into SWC, but I couldn't understand how it handles UTF-8 strings because it seems the input api needs to develop self to handle correct UFT-8 index.

use swc_common::input::{StringInput, Input};use swc_common::BytePos;fn main() {    let utf8_str = "中文字串";    let mut input =  StringInput::new("中文字串", BytePos(0), BytePos(utf8_str.len().try_into().unwrap()));    println!("{:?}", input.slice(BytePos(0), BytePos(3)));    println!("{:?}", &utf8_str[0..3]);   // is there any function like slice(start_usize, end_usize) can get utf-8 string }

Viewing all articles
Browse latest Browse all 1134

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>