Rust:string and &str_rust

Source: Internet
Author: User

Rust Official Explanation:
"String is a byte buffer that is owned (owned) UTF-8 allocated on the heap. Variable strings can be modified to increase their capacity as needed.

&STR is a fixed-capacity "view" of a String that is assigned somewhere. If a slice is extracted from a string, it is usually pointed to the heap and, if it is a string literal, points to static memory.

&str is a native type implemented by the rust language, and String is implemented by the standard library. ”

For rust, "string" is a stream with a sequence of Unicode scalar values encoded as Utf-8 bytes. All strings must be guaranteed to be valid UTF-8 encoding sequences.

But, more than that, Rust's handling of strings has some personality.

Let a = "Hello World";
Let B = "ok!";

1, the literal "string" is &str

A, B are &str, not string.

2, realize the conversion of &STR =>string

Let C =a.to_string (); Method A let

D =string::from (b)//Method two

or let D =a.to_owned ();

3, the implementation of string =>&STR conversion: Add A &

Let E = &string::from ("Hello World"); 

or use AS_STR ();

Let E_0 = String::from ("Hello World"); 
Let e   = E_0.as_str ();

But: Can not directly

4, a string+ a &str =>string

Note that, first of all, String, then N &str.

Let f = c+b;
or let F =c+b+e;//add one also can let
g = C.clone () +b+e

Of course, the other way is: Push_str ()

    Let mut strs = "Hello". to_string ();
    Strs.push_str ("world!"); Hello world!
    println! ("State1: {:?}", STRs);

    Let mut s = String::from ("foo");
    S.push_str ("Bar"); Foobar
    println! (" {:?} ", s);

5, how two or more strings become a string.

Let H = C +&d; 

If you want multiple, for example:

Or

Let i = C.clone () + &d;//is of little significance

Or

Let J = format! ("{0}{1}", A, b);

6, for example, a f32, a string, to become a new string,

Let num =2008_u32;
Let MyStr =string::from (", Happy new year!");
Let Num_str = format! ("{0}{1}", Num, mystr);//2008, Happy new year!

Recommend this way, more convenient, flexibility is also relatively strong. Other ways are also possible. Let

num1 =200_u32;
Let num2 =8_u32;
Let MyStr2 = "F32" Let
new_num_str =format! (" {0} {1}_{2} ", NUM1,NUM2,MYSTR2);//2008_f32

7, about As_bytes (); Chars ();

Here's a reminder that characters and byte are not simple one by one counterparts.

    Let Love_china = "loyal dog ハチ male";
    for (NUM, BT) in Love_china.as_bytes (). ITER (). Enumerate () {
        println! () length:{:?} num:{:?} byte:{:?} ", Love_china.len (), Num, BT);

    for (num, ch) in Love_china.chars (). Enumerate () {
        println! () length:{:?} num:{:?} char:{:?} ", Love_china.len (), num, ch);
    

8, Slice, []

&str,string can not be directly slice.

    Let Mystr:&str = "ABCD";
    Let my_str:string = "ABCD". to_string ();
    println! ("MyStr: {:?}", mystr[1.]);   =>error
    println! (" MyStr: {:?} ", &mystr[1..2]);   "BC"
    println! (" MyStr: {:?} ", &mystr[1.]);   "BCD"
    println! (" MyStr: {:?} ", &mystr[.]);    "ABCD"
    //Obviously, a string cannot be directly slice
    //println! (" MY_STR: {:?} ", my_str[1.]);  =>error
    println! (" MY_STR: {:?} ", &my_str[1..2]); "BC"
    println! (" MY_STR: {:?} ", &my_str[1.]); "BCD"
    println! (" MY_STR: {:?} ", &my_str[.]);  "ABCD"

9, about the &STR internal address

Let mystr: &str = "ABCD";
println! ("MyStr address  :  {:p}", &mystr[1.]); /starting from the 2nd
println! (" MyStr Address  :  {:p} ", &mystr[2.]);
println! ("MyStr address  :  {:p}", &mystr[3.]);

Output

MyStr Address:  0x7ff6ffcd0612 mystr Address:  0x7ff6ffcd0613
mystr  Address:  0x7ff6ffcd0614

What you find.

10. extend:string Aggregate Char

    Let Mut message = String::from ("The" the "the" "the" the "the" the "the" three letters are

    Message.extend (&[' A ', ' B ', ' C ']);

    println! ("message:{:?}", message);//message: "The Three letters ARE:ABC"

11, string of memory distribution

Let S1 =string::from ("Hello");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.