【百例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
相关推荐
小黄人软件4 分钟前
C++读写编辑CSV文件示例源码 用于数据导入导出,比Excel好使
开发语言·c++·excel
郭涤生11 分钟前
C++各个版本的性能和安全性总结
开发语言·c++
西门吹-禅42 分钟前
【Rust setup】
rust·rust setup
小村儿1 小时前
给 AI Agent 装上"长期记忆":Karpathy 的 LLM Wiki 思想,我做成了工具
前端·后端·ai编程
wljy11 小时前
二、静态库的制作和使用
linux·c语言·开发语言·c++
何陋轩1 小时前
Spring AI实战指南:在Java项目中集成大语言模型
人工智能·后端·机器学习
道剑剑非道2 小时前
FFmpeg 6.0 实战:用 C++ 封装摄像头采集与 RTSP 推流
开发语言·c++·ffmpeg
天天进步20152 小时前
Python全栈项目实战:基于深度学习的语音合成(TTS)系统
开发语言·python·深度学习
OctShop大型商城源码2 小时前
.NET线上商城源码_C#商城源码_技术赋能下的电商新生态
开发语言·c#·.net·商城系统源码
用户8356290780512 小时前
Python 操作 PowerPoint 表格的创建与格式化
后端·python