How to Operate Elements of String in Rust?

#1

In C, we can operate a string by index like this char str[5] = “abcde”; str[i] … … or by pointer; Is there a similar mechanism in Rust? If not, how to operate a string in Rust?

0 Likes

#2

You can’t do that with strings in rust, because the components are not of fixed size, so it does not behave like an array. You can create a Vec<char> if you want to use it like an array. NB Rust char is not the same thing as a C char.

0 Likes

#4

thx, and if I write a string like this: let s = “A STRING”.to_string(); then I use: s.as_slice() , does it mean ‘s’ itself is converted to &str type? or I only get a &str copy of ‘s’?

0 Likes

#5

Please use Stack Overflow to ask these kinds of questions. This forum is for discussion about the development of the language itself, not using it.

0 Likes

closed #6

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.

0 Likes