rust 初探 -- 切片

rust 初探 – 切片

切片

  • rust 的另外一种不持有所有权的数据类型:切片(slice)
    示例:
fn main() {
    let mut s = String::from("hello world");
    let world_index = first_world(&s);

    s.clear(); 
    //这里清空之后,因为 world_index 和字符串没有关联,导致实际失去了有效性
    //需要保证 world_index 和字符串直接的关联,保证其有效性
    println!("{}", world_index);
}

fn first_world(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 s = String::from("hello world");
    let hello = &s[..5];	//&s[..0], 整个字符串的切片: &s[..]
    let world = &s[6..11];//&s[6..s.len()]
    println!("{}, {}", hello, world);
}

在这里插入图片描述

注意

  • 字符串切片的范围索引必须发生在有效的UTF-8 字符边界内
  • 如果尝试从一个多字节的字符中创建字符串切片,程序会报错并退出

字符串切片重写示例:

fn main() {
    let mut  s = String::from("hello world");
    let world_index = first_world(&s);

    s.clear();
    println!("{}", world_index);
}

fn first_world(s: &String) -> &str {
    let bytes = s.as_bytes();
    for (i, &item) in bytes.iter().enumerate() {
        if item == b' ' {
            return &s[..i];
        }
    }
    &s[..]
}
/*
error[E0502]: cannot borrow `s` as mutable because it is also borrowed as immutable
  --> src/main.rs:18:5
   |
16 |     let world_index = first_world(&s);
   |                                   -- immutable borrow occurs here
17 |
18 |     s.clear();
   |     ^^^^^^^^^ mutable borrow occurs here
19 |     println!("{}", world_index);
   |                    ----------- immutable borrow later used here
*/
相关知识点
  • 字符串字面值是被直接存储在二进制程序中
  • let s = “hello”;
  • 变量 s 的类型是 &str,它是一个指向二进制程序特定位置的切片,&str 是不可变的,所以字符串字面值也是不可变的

将字符串切片作为参数传递

  • fn first_world(s: &String) -> &str {
  • 最好是使用 &str 作为参数类型进行传递,因为这样函数既可以接收 String(使用切片) 和 &str 类型的参数了
fn main() {
    let s = String::from("hello world");
    let world_index = first_world(&s[..]);

    println!("{}", world_index);
}

fn first_world(s: &str) -> &str {
    let bytes = s.as_bytes();
    for (i, &item) in bytes.iter().enumerate() {
        if item == b' ' {
            return &s[..i];
        }
    }
    &s[..]
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Mac Rust io-uring是一种在Mac操作系统上使用Rust语言进行开发的io-uring库。 io-uring是Linux内核中的一个新特性,它为应用程序提供了一种高性能、高效率的异步I/O操作方式。它通过使用事件驱动和无锁技术,实现了在高并发环境下进行文件操作的优化。io-uring提供了更低的系统开销和更高的吞吐量,特别适用于需要大量I/O操作的应用程序。 虽然io-uring最初是为Linux内核设计的,但由于其高性能的特性,一些开发者试图将其移植到其他操作系统上。其中,Mac Rust io-uring就是一个在Mac操作系统上使用Rust语言实现io-uring的库。 使用Mac Rust io-uring,开发者可以在Mac环境下利用io-uring的特性来提高文件操作的性能。这对于需要进行大量I/O操作的应用程序来说,是一个很有价值的工具。例如,对于数据库、Web服务器或文件传输等应用,通过使用Mac Rust io-uring,可以显著提高其性能和吞吐量。 Mac Rust io-uring不仅提供了对io-uring的封装,还提供了一些更高级别的功能和接口,以方便开发者使用。开发者可以使用Mac Rust io-uring来实现一些高级的文件操作,例如批量读取或写入文件,提高数据处理的效率。 总之,Mac Rust io-uring是一个在Mac操作系统上使用Rust语言开发的io-uring库,它能够为开发者提供高性能的异步I/O操作方式,从而提高应用程序的性能和吞吐量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jiangw557

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值