[rust-dev] Using String and StrSlice

Kang Seonghoon someone at mearie.org
Sun Jun 1 21:32:54 PDT 2014


If your string is an ASCII, you can convert between `String` and
`Vec<Ascii>` using `to_ascii()` and `as_str_ascii()` methods. You can
then update an individual character in the vector. See `std::ascii`
for related traits.

Codepoint-wise operation is prone to error and I don't think such
methods would be added in the future. In general, do not think
`String` as a sequence of bytes or codepoints (Unicode scalar values
to be exact) or characters. Think it as a black box containing some
human text. `chars()` (a misnomer!) and `as_bytes()` should be used to
extract a list of codepoints and UTF-8 bytes from the string.
Codepoint-wise operations can always be done with `Vec<char>`.

2014-06-02 3:20 GMT+09:00 Christophe Pedretti <christophe.pedretti at gmail.com>:
> Hi all,
>
> suppose i want to replace the i th character c (this character is ascii, so
> represented by exactly one byte) in a String named buf with character 'a'
>
> i can do this
> buf =
> buf.as_slice().slice_to(i).to_string().append("a").append(buf.as_slice().slice_from(i+1))
>
> if c is any UTF8 character, i can use
> buf =
> buf.as_slice().slice_to(i).to_string().append("a").append(buf.as_slice().slice_from(i+c.len_utf8_bytes()))
>
> It's quite complex, no better way to do ?
>
> Any future additional methods for String are planned ?
>
> Thanks
>
> --
> Christophe
>
>
>
>
>
> _______________________________________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>



-- 
-- Kang Seonghoon | Software Engineer, iPlateia Inc. | http://mearie.org/
-- Opinions expressed in this email do not necessarily represent the
views of my employer.
--


More information about the Rust-dev mailing list