rust之String、str、‘static str 与 FastStr使用详解

news/2025/2/16 1:00:36/

关注我,学习Rust不迷路!!

String:

  1. 创建一个空的 String
let empty_string = String::new();
  1. 从字符串字面量创建一个 String
let hello_string = String::from("Hello");
  1. 将一个字符串追加到现有的 String
let mut name = String::from("John");
name.push_str(" Doe");
  1. String 转换为 str 切片:
let name = String::from("John");
let name_slice: &str = &name;
  1. 连接两个 String 对象:
let first_name = String::from("John");
let last_name = String::from("Doe");
let full_name = first_name + " " + &last_name;
  1. 重复一个 String 多次:
let repeated_string = "abc".repeat(3);
  1. 检查一个 String 是否包含子字符串:
let message = String::from("Hello, World!");
let contains_hello = message.contains("Hello");
  1. 将数字值转换为 String
let number = 42;
let number_string = number.to_string();
  1. 根据分隔符将 String 拆分为子字符串:
let message = String::from("Hello, World!");
let words: Vec<&str> = message.split(", ").collect();
  1. String 转换为大写:
let message = String::from("hello");
let uppercase_message = message.to_uppercase();

str:

  1. 从字符串字面量创建一个字符串切片:
let name: &str = "hello";
  1. 检查一个字符串切片是否为空:
let empty: &str = "";
let is_empty = empty.is_empty();
  1. 比较两个字符串切片:
let name1: &str = "John";
let name2: &str = "Doe";
let is_equal = name1 == name2;
  1. 获取字符串切片的长度:
let name: &str = "John";
let length = name.len();
  1. 检查字符串切片是否以某个前缀开头:
let name: &str = "John";
let starts_with_j = name.starts_with("J");
  1. 在字符串切片中查找子字符串的索引:
let message: &str = "Hello, World!";
let index = message.find("World");
  1. 将字符串切片转换为小写:
let message: &str = "HELLO";
let lowercase_message = message.to_lowercase();
  1. 反转字符串切片:
let message: &str = "Hello";
let reversed_message: String = message.chars().rev().collect();
  1. 去除字符串切片中的空白字符:
let message: &str = "  Hello, World!  ";
let trimmed_message = message.trim();
  1. 根据分隔符将字符串切片拆分为子字符串:
let message: &str = "Hello, World!";
let words: Vec<&str> = message.split(", ").collect();

&'static str:

  1. 创建一个静态字符串切片:
static HELLO: &str = "Hello";
  1. 将静态字符串切片用作函数参数:
fn print_message(message: &'static str) {println!("{}", message);
}
  1. 将静态字符串切片与字符串切片进行比较:
static HELLO: &str = "Hello";
let name: &str = "John";
let is_equal = HELLO == name;
  1. 从函数返回一个静态字符串切片:
fn get_greeting() -> &'static str {"Hello, World!"
}
  1. 将静态字符串切片与字符串切片连接:
static HELLO: &str = "Hello";
let name: &str = "John";
let greeting = HELLO.to_owned() + ", " + name;
  1. 在match表达式中使用静态字符串切片:
static GREETING: &str = "Hello";
match name {"John" => println!("{}", GREETING),_ => println!("Unknown"),
}
  1. 将静态字符串切片传递给接受字符串切片的函数:
fn print_message(message: &str) {println!("{}", message);
}
print_message(HELLO);
  1. 在结构体中存储静态字符串切片:
struct Person {name: &'static str,
}
let person = Person { name: HELLO };
  1. 将静态字符串切片作为函数参数的默认值:
fn print_message(message: &str) {println!("{}", message);
}
fn greet(name: &str, message: &str) {let greeting = format!("{} {}", message, name);print_message(&greeting);
}
greet("John", HELLO);
  1. 将静态字符串切片用作常量值:
const PI: f64 = 3.14159;
static GREETING: &str = "Hello";

fast_str crate:

请注意, fast_str crate是一个第三方库,需要在Cargo.toml文件中添加依赖才能使用。下面是一些可能的使用方式的示例:

  1. 在Cargo.toml中添加 fast_str crate的依赖:
[dependencies]
fast_str = "0.1.0"
  1. 使用 fast_str crate中的 FastString 类型创建一个空的字符串:
use fast_str::FastString;
let empty_string = FastString::new();
  1. 从字符串字面量创建一个 FastString 对象:
use fast_str::FastString;
let hello_string = FastString::from("Hello");
  1. 将一个字符串追加到现有的 FastString 对象:
use fast_str::FastString;
let mut name = FastString::from("John");
name.push_str(" Doe");
  1. FastString 转换为 &str 切片:
use fast_str::FastString;
let name = FastString::from("John");
let name_slice: &str = &name;
  1. 连接两个 FastString 对象:
use fast_str::FastString;
let first_name = FastString::from("John");
let last_name = FastString::from("Doe");
let full_name = first_name + " " + &last_name;
  1. 重复一个 FastString 对象多次:
use fast_str::FastString;
let repeated_string = FastString::from("abc").repeat(3);
  1. 检查一个 FastString 是否包含子字符串:
use fast_str::FastString;
let message = FastString::from("Hello, World!");
let contains_hello = message.contains("Hello");
  1. 将数字值转换为 FastString
use fast_str::FastString;
let number = 42;
let number_string = FastString::from(number);
  1. 根据分隔符将 FastString 拆分为子字符串:
use fast_str::FastString;
let message = FastString::from("Hello, World!");
let words: Vec<&str> = message.split(", ").collect();

关注我,学习Rust不迷路!!


http://www.ppmy.cn/news/1009432.html

相关文章

WebAssembly

WebAssembly&#xff08;简称Wasm&#xff09;是一种面向Web的二进制指令格式&#xff0c;用于在现代Web浏览器中运行高性能的可移植代码。它是一种跨平台、低级别的虚拟机技术&#xff0c;允许开发者将不同编程语言的代码编译成Wasm格式&#xff0c;然后在Web浏览器中运行。 …

C++智能指针weak_ptr

文章目录 weak_ptr概述weak_ptr常用操作尺寸问题 weak_ptr概述 weak_ptr概述 weak_ptr辅助shared_ptr进行工作,weak_ptr是弱引用的意思(强引用是shared_ptr), weak_ptr同shared_ptr一样都是类模板和智能指针,weak_ptr对资源的引用是非拥有式的,weak_ptr没有资源的管理权限,不能…

java反射机制原理、获取Class方式和其应用场景

1、反射是什么&#xff1a; 反射是一种动态地获取和操作类信息的行为。类信息包括类的属性、方法、构造函数等。 类信息在Java中通常存储在.class文件中。当我们编写Java代码并进行编译时&#xff0c;编译器&#xff08;javac&#xff09;将源代码转换为字节码&#xff0c;并将…

MongoDB SQL

Microsoft Windows [版本 6.1.7601] 版权所有 (c) 2009 Microsoft Corporation。保留所有权利。C:\Users\Administrator>cd C:\MongoDB\Server\3.4\binC:\MongoDB\Server\3.4\bin> C:\MongoDB\Server\3.4\bin> C:\MongoDB\Server\3.4\bin>net start MongoDB 请求的…

第七章:进程间通信(IPC)——构成进程间通信的信道方案

系列文章目录 文章目录 系列文章目录前言进程间通信介绍进程间通信目的进程间通信发展进程间通信分类进程通信的原理 管道什么是管道pipe管道通信特点简单设计 命名管道什么是命名管道mkfifostrcmp/strncasecmpunlinkgetch简单设计 共享内存什么是共享内存shmget/ftokipcsshmct…

程序环境和预处理(含C语言程序的编译+链接)--2

文章前言&#xff1a; 上章我们把 程序的翻译环境 程序的执行环境 C语言程序的编译链接 预定义符号介绍 预处理指令 #define 宏和函数的对比 预处理操作符 #和##的介绍 的相关知识进行了梳理讲解&#xff0c;接下来被把剩余知识 命令定义 …

android studio 实用插件推荐

本文字数&#xff1a;&#xff1a;2352字 预计阅读时间&#xff1a;8分钟 背景 现在做安卓开发的同学基本都是用 Android Studio 了吧&#xff0c;它具有强大的开放性&#xff0c;可以让用户根据自己的需求开发或使用一些插件辅助自己搬砖&#xff0c;当然开发插件我们可能还没…

2023年中小型企业如何选择CRM?

随着科技的不断发展&#xff0c;CRM软件已经成为企业管理的重要工具之一。但是对于中小企业来说&#xff0c;选择一款适合自己企业的CRM软件并不容易。2023年有哪些适合中小企业的crm软件推荐&#xff1f;下面3款crm让企业轻松管理客户&#xff0c;提升企业效率。 1.Zoho CRM …