zoukankan      html  css  js  c++  java
  • 2.6 Rust Slice Type

    字符串操作

    fn first_word(s: &String) -> usize {
        let bytes = s.as_bytes();
    
        for (i, &item) in bytes.iter().enumerate() {
            if item == b' ' {
                return i;
            }
        }
    
        s.len()
    }

    Because we need to go through the String element by element and check whether a value is a space, we’ll convert our String to an array of bytes using the as_bytes method

     For now, know that iter is a method that returns each element in a collection and that enumerate wraps the result of iter and returns each element as part of a tuple instead. The first element of the tuple returned from enumerate is the index, and the second element is a reference to the element. This is a bit more convenient than calculating the index ourselves.

    Because the enumerate method returns a tuple, we can use patterns to destructure that tuple, just like everywhere else in Rust. So in the for loop, we specify a pattern that has i for the index in the tuple and &item for the single byte in the tuple. Because we get a reference to the element from .iter().enumerate(), we use & in the pattern.

    Inside the for loop, we search for the byte that represents the space by using the byte literal syntax. If we find a space, we return the position. Otherwise, we return the length of the string by using s.len()

    fn main() {
        let mut s = String::from("hello world");
    
        let _word = first_word(&s); // word will get the value 5
    
        println!("{},{}",s,_word);
        s.clear(); // this empties the String, making it equal to ""
    
        // word still has the value 5 here, but there's no more string that
        // we could meaningfully use the value 5 with. word is now totally invalid!
    }
    
    
    fn first_word(s: &String) -> usize {
        let bytes = s.as_bytes();
    
        for (i, &item) in bytes.iter().enumerate() {
            if item == b' ' {
                return i;
            }
        }
    
        s.len()
    }

    字符串切片

    str[index..end_position]

    index:字符索引位置,从0开始

    end_position:字符位置,从1开始

    let s = String::from("hello world");
    
    let hello = &s[0..5];
    let world = &s[6..11];

    With Rust’s .. range syntax, if you want to start at the first index (zero), you can drop the value before the two periods. In other words, these are equal:

    let s = String::from("hello");
    
    let slice = &s[0..2];
    let slice = &s[..2];

    By the same token, if your slice includes the last byte of the String, you can drop the trailing number. That means these are equal:

    let s = String::from("hello");
    
    let len = s.len();
    
    let slice = &s[3..len];
    let slice = &s[3..];

    You can also drop both values to take a slice of the entire string. So these are equal:

    let s = String::from("hello");
    
    let len = s.len();
    
    let slice = &s[0..len];
    let slice = &s[..];

    Note: String slice range indices must occur at valid UTF-8 character boundaries. If you attempt to create a string slice in the middle of a multibyte character, your program will exit with an error.

     切片字符串操作

    fn first_word(s: &String) -> &str {
        let bytes = s.as_bytes();
    
        for (i, &item) in bytes.iter().enumerate() {
            if item == b' ' {
                return &s[0..i];
            }
        }
    
        &s[..]
    }

    第一种方式为直接对字符串本身进行操作,没有第二个变量产生,切片则是新定义了一个变量,指向了原字符串的部分内容。当作用域发生变化时,它们的不同就会显现出来。

     下面这一段代码是正确的

    fn main() {
        let mut s = String::from("hello world");
    
        let word = first_word(&s);
    
        s.clear(); // error!
    
        println!("the first word is: {}", word);
    }
    
    fn first_word(s: &String) -> usize {
        let bytes = s.as_bytes();
    
        for (i, &item) in bytes.iter().enumerate() {
            if item == b' ' {
                return i;
            }
        }
    
        s.len()
    }

    然而官网上则说这段代码是有问题的,可能是rust版本升级后此处做了改动

    fn main() {
        let my_string = String::from("hello world");
    
        // first_word works on slices of `String`s
        let word = first_word(&my_string);
    
        //let my_string_literal = "hello world";
    
        // first_word works on slices of string literals
        //let word = first_word(&my_string_literal);
    
        // Because string literals *are* string slices already,
        // this works too, without the slice syntax!
        //let word = first_word(my_string_literal);
        println!("{}",word);
    }
    
    fn first_word(s: &String) -> &str {
        let bytes = s.as_bytes();
    
        for (i, &item) in bytes.iter().enumerate() {
            if item == b' ' {
                return &s[0..i];
            }
        }
    
        &s[..]
    }
  • 相关阅读:
    截取最后一个下划线前面的字符串
    jqgrid加载本地数据功能
    Android、Ios手机端字体根据屏幕分辨率自适应的方法,使用rem和px的区别
    js获取8个月前时间,1天前时间
    手机端/pc端 弹出后,禁止底部页面滚动方法
    列表左右滚动
    jQuery点击隐藏点击显示,计算高度,位置,给当前加上焦点,其他去掉焦点
    工厂模式浅析
    教你看懂UML类图
    Rpc基础篇
  • 原文地址:https://www.cnblogs.com/perfei/p/10695393.html
Copyright © 2011-2022 走看看