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

关注我,学习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不迷路!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值