【百例RUST - 010】字符串

【百例RUST - 010】字符串

第一章 字符串定义

第01节 创建空的字符串

案例代码

rust 复制代码
fn main(){
    // 创建一个空的字符串
    let mut msg1: String = String::new();

    // 向字符串当中添加一个数据
    msg1.push_str("hello");

    // 输出数据
    println!("{}", msg1);
}

// hello

第02节 from创建字符串

案例代码

rust 复制代码
fn main(){
    // 创建一个空的字符串
    let msg2: String = String::from("hello");
 
    // 输出数据
    println!("{}", msg2);
}

// hello

第03节 str创建字符串

案例代码

rust 复制代码
fn main(){
    // 创建一个空的字符串
    let msg3: String = "helloworld".to_string();
 
    // 输出数据
    println!("{}", msg3);
}

// helloworld

第二章 字符串操作

第01节 更新字符串

案例代码,采用 push_str

rust 复制代码
fn main(){
    // 创建一个空的字符串
    let mut msg1: String = String::from("hello");
    // 更新字符串
    msg1.push_str("world");

    let msg2: String = "rust".to_string();

    msg1.push_str(&msg2);
    // 输出数据
    println!("{}", msg1);
    println!("{}", msg2);
}

// helloworld
// rust

需要注意的是,这里的 msg1msg2 并没有失去所有权。

案例代码 采用 push

rust 复制代码
fn main(){
    // 创建一个空的字符串
    let mut msg1: String = String::from("hello");

    // 更新字符
    msg1.push('A');
    // 输出数据
    println!("{}", msg1); 
}

// helloA

需要注意的是,上面的 push 只能采用单引号,添加一个字符,其他情况均报错

第02节 字符串的加号

案例代码

rust 复制代码
fn main(){
    // 创建一个空的字符串
    let mut msg1: String = String::from("hello");
    let mut msg2: String = "world".to_string();

    // 更新字符串
    let msg3 = msg1 + &msg2;
    // 输出数据
    // println!("{}", msg1); 
    println!("{}", msg2); 
    println!("{}", msg3); 
}

// 需要注意的是 msg1 已经失去了所有权, 将所有权交给了 msg3 所以 msg1 将无法使用!
// world
// helloworld

第03节 字符串的format

案例代码

rust 复制代码
fn main(){
    // 创建一个空的字符串
    let  msg1: String = String::from("hello");
    let  msg2: String = String::from("world");
    let  msg3: String = String::from("rust");
    let  msg : String  = format!("{}-{}-{}", msg1, msg2, msg3);

    // 输出数据
    println!("{}", msg1); 
    println!("{}", msg2); 
    println!("{}", msg3); 
    println!("{}", msg); 
}

// hello
// world
// rust
// hello-world-rust

对于 format 的解释 format!("{}-{}-{}", msg1, msg2, msg3)

复制代码
1、内存分配与返回值的问题
	format! 的工作流程,可以看作是一个 "工厂制作" 的过程。
	A、分配空间: 他会在 堆(Heap)上面开辟一块全新的内存空间。
	B、内容拷贝: 它按照你指定的格式 {}-{}-{}  将字符串 msg1, msg2, msg3 的内容拷贝到这个新的内存中。
	C、返回对象: 它会返回一个全新的 String 对象给变量 msg
	需要注意的是,这里的 msg 是一个独立的实体,修改 msg 不会影响到 msg1,  反之亦然。 它们在内存当中是完全分开的。
	
2、形象化的理解
	我们可以想象 msg1, msg2, msg3 是三本书。
	A、执行 format! 就像是拿着这三本书去  复印机上复印。
	B、复印机会根据我们的要求(中间加上横杠) 印出一本新的书 msg
	C、结果: 原本的三本书,还在我们的书架上面。(可以继续 println!) 而且我们手上多了一本缝合好的新书。

第三章 获取子字符

第01节 字符串切片

rust 复制代码
fn main(){
    // 创建一个空的字符串
    let  msg: String = String::from("你好");
     
     // 通过索引获取字符串当中的数据
    let element = &msg[0..3];

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

// 你

最快。O(1) 复杂度,但若索引切断了字符会触发崩溃。 因为 UTF-8 中,中文占据 3个字节的大小

第02节 字符获取

案例代码

rust 复制代码
fn main(){
    // 创建一个空的字符串
    let  msg: String = String::from("你好");
     
     // 通过索引获取字符串当中的数据
     let element: Option<char> = msg.chars().nth(0);
     match element {
         None => println!("数据不合法"),
         Some(x) => println!("{}", x),
     }
}

// 你

最安全。处理 Unicode 友好,但是时间复杂度较高 O(n)

第四章 字符串的遍历

第01节 字符的遍历方式

案例代码 核心函数 chars()

rust 复制代码
fn main(){
    // 创建一个空的字符串
    let  msg: String = String::from("你好");
     
     // 采用字符的方式进行遍历 chars
     for element in msg.chars(){
        println!("{}", element)
     }
}

//  你
//  好

第02节 字节的遍历方式

案例代码 核心函数 bytes()

rust 复制代码
fn main(){
    // 创建一个空的字符串
    let  msg: String = String::from("你好");
     
     // 采用字符的方式进行遍历 bytes
     for element in msg.bytes(){
        println!("{}", element)
     }
}

//  228
//  189
//  160
//  229
//  165
//  189
相关推荐
赵侃侃爱分享3 小时前
学完Python第一次写程序写了这个简单的计算器
开发语言·python
断眉的派大星3 小时前
# Python 魔术方法(魔法方法)超详细讲解
开发语言·python
2501_933329553 小时前
技术深度拆解:Infoseek舆情处置系统的全链路架构与核心实现
开发语言·人工智能·自然语言处理·架构
妮妮喔妮3 小时前
supabase的webhook报错
开发语言·前端·javascript
我的xiaodoujiao3 小时前
API 接口自动化测试详细图文教程学习系列11--Requests模块3--测试练习
开发语言·python·学习·测试工具·pytest
xiaoye-duck3 小时前
【C++:C++11】C++11新特性深度解析:从类新功能、Lambda表达式到包装器实战
开发语言·c++·c++11
qq_12084093713 小时前
Three.js 大场景分块加载实战:从全量渲染到可视集调度
开发语言·javascript·数码相机
csbysj20203 小时前
Pandas 常用函数
开发语言
无心水3 小时前
OpenClaw技术文档/代码评审/测试用例生成深度实战
网络·后端·架构·测试用例·openclaw·养龙虾