7.字符串.rs

/*

*/

fn test_1() {
     //新建字符串:
     let string = String::new();

     //字符串长度:
     let len = string.len();

     //基础类型转换成字符串:
     let one = 1.to_string(); // 整数到字符串
     let float = 1.3.to_string(); // 浮点数到字符串
     let slice = "slice".to_string(); // 字符串切片到字符串

     let my_int = one.parse::<i32>().unwrap();
     let my_float = float.parse::<f32>().unwrap();
     let my_slice = slice.as_str();
}

//包含 UTF-8 字符的字符串:
fn test_2() {
     let hello = String::from("السلام عليكم");
     let hello = String::from("Dobrý den");
     let hello = String::from("Hello");
     let hello = String::from("שָׁלוֹם");
     let hello = String::from("नमस्ते");
     let hello = String::from("こんにちは");
     let hello = String::from("안녕하세요");
     let hello = String::from("你好");
     let hello = String::from("Olá");
     let hello = String::from("Здравствуйте");
     let hello = String::from("Hola");
}

//字符串追加:
fn test_3() {
     let mut s = String::from("run");
     s.push_str("oob"); // 追加字符串切片
     s.push('!'); // 追加字符

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

//用 + 号拼接字符串:
fn test_4() {
     let s1 = String::from("Hello, ");
     let s2 = String::from("world!");
     let s3 = s1 + &s2;
     println!("{}", s3);
}

//这个语法也可以包含字符串切片:
fn test_5() {
     let s1 = String::from("tic");
     let s2 = String::from("tac");
     let s3 = String::from("toe");
     let s = s1 + "-" + &s2 + "-" + &s3;
     println!("{}", s);
}

//使用 format! 宏:
fn test_6() {
     let s1 = String::from("tic");
     let s2 = String::from("tac");
     let s3 = String::from("toe");
     let s = format!("{}-{}-{}", s1, s2, s3);
     println!("{}", s);
}

//字符串长度:
fn test_7() {
     let s = "hello";
     let len = s.len(); //5

     let s = "你好";
     let len = s.len(); //这里 len 的值是 6。因为中文是 UTF-8 编码的,每个字符长 3 字节,所以长度为6。
     let len = s.chars().count(); //如果想统计字符数量可以先取字符串为字符集合:

     let s = "hello你好";
     let len = s.chars().count();//7
}

fn main() {
     test_1();
     test_2();
     test_3();
     test_4();
     test_5();
     test_6();
     test_7();
}

 

已标记关键词 清除标记
相关推荐
<p> <b><span style="background-color:#FFE500;">【超实用课程内容】</span></b> </p> <p> <br /> </p> <p> <br /> </p> <p> 本课程内容包含讲解<span>解读Nginx的基础知识,</span><span>解读Nginx的核心知识、带领学员进行</span>高并发环境下的Nginx性能优化实战,让学生能够快速将所学融合到企业应用中。 </p> <p> <br /> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <b><br /> </b> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <b><span style="background-color:#FFE500;">【课程如何观看?】</span></b> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> PC端:<a href="https://edu.csdn.net/course/detail/26277"><span id="__kindeditor_bookmark_start_21__"></span></a><a href="https://edu.csdn.net/course/detail/27216">https://edu.csdn.net/course/detail/27216</a> </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 移动端:CSDN 学院APP(注意不是CSDN APP哦) </p> <p style="font-family:Helvetica;color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 本课程为录播课,课程永久有效观看时长,大家可以抓紧时间学习后一起讨论哦~ </p> <p style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <br /> </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <strong><span style="background-color:#FFE500;">【学员专享增值服务】</span></strong> </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> <b>源码开放</b> </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 课件、课程案例代码完全开放给你,你可以根据所学知识,自行修改、优化 </p> <p class="ql-long-24357476" style="font-family:"color:#3A4151;font-size:14px;background-color:#FFFFFF;"> 下载方式:电脑登录<a href="https://edu.csdn.net/course/detail/26277"></a><a href="https://edu.csdn.net/course/detail/27216">https://edu.csdn.net/course/detail/27216</a>,播放页面右侧点击课件进行资料打包下载 </p> <p> <br /> </p> <p> <br /> </p> <p> <br /> </p>
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页