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

String:

创建一个空的 String :

let empty_string = String::new();

从字符串字面量创建一个 String :

let hello_string = String::from("Hello");

将一个字符串追加到现有的 String :

let mut name = String::from("John");

name.push_str(" Doe");

将 String 转换为 str 切片:

let name = String::from("John");

let name_slice: &str = &name;

连接两个 String 对象:

let first_name = String::from("John");

let last_name = String::from("Doe");

let full_name = first_name + " " + &last_name;

重复一个 String 多次:

let repeated_string = "abc".repeat(3);

检查一个 String 是否包含子字符串:

let message = String::from("Hello, World!");

let contains_hello = message.contains("Hello");

将数字值转换为 String :

let number = 42;

let number_string = number.to_string();

根据分隔符将 String 拆分为子字符串:

let message = String::from("Hello, World!");

let words: Vec<&str> = message.split(", ").collect();

将 String 转换为大写:

let message = String::from("hello");

let uppercase_message = message.to_uppercase();

str:

从字符串字面量创建一个字符串切片:

let name: &str = "hello";

检查一个字符串切片是否为空:

let empty: &str = "";

let is_empty = empty.is_empty();

比较两个字符串切片:

let name1: &str = "John";

let name2: &str = "Doe";

let is_equal = name1 == name2;

获取字符串切片的长度:

let name: &str = "John";

let length = name.len();

检查字符串切片是否以某个前缀开头:

let name: &str = "John";

let starts_with_j = name.starts_with("J");

在字符串切片中查找子字符串的索引:

let message: &str = "Hello, World!";

let index = message.find("World");

将字符串切片转换为小写:

let message: &str = "HELLO";

let lowercase_message = message.to_lowercase();

反转字符串切片:

let message: &str = "Hello";

let reversed_message: String = message.chars().rev().collect();

去除字符串切片中的空白字符:

let message: &str = " Hello, World! ";

let trimmed_message = message.trim();

根据分隔符将字符串切片拆分为子字符串:

let message: &str = "Hello, World!";

let words: Vec<&str> = message.split(", ").collect();

&'static str:

创建一个静态字符串切片:

static HELLO: &str = "Hello";

将静态字符串切片用作函数参数:

fn print_message(message: &'static str) {

println!("{}", message);

}

将静态字符串切片与字符串切片进行比较:

static HELLO: &str = "Hello";

let name: &str = "John";

let is_equal = HELLO == name;

从函数返回一个静态字符串切片:

fn get_greeting() -> &'static str {

"Hello, World!"

}

将静态字符串切片与字符串切片连接:

static HELLO: &str = "Hello";

let name: &str = "John";

let greeting = HELLO.to_owned() + ", " + name;

在match表达式中使用静态字符串切片:

static GREETING: &str = "Hello";

match name {

"John" => println!("{}", GREETING),

_ => println!("Unknown"),

}

将静态字符串切片传递给接受字符串切片的函数:

fn print_message(message: &str) {

println!("{}", message);

}

print_message(HELLO);

在结构体中存储静态字符串切片:

struct Person {

name: &'static str,

}

let person = Person { name: HELLO };

将静态字符串切片作为函数参数的默认值:

fn print_message(message: &str) {

println!("{}", message);

}

fn greet(name: &str, message: &str) {

let greeting = format!("{} {}", message, name);

print_message(&greeting);

}

greet("John", HELLO);

将静态字符串切片用作常量值:

const PI: f64 = 3.14159;

static GREETING: &str = "Hello";

fast_str crate:

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

在Cargo.toml中添加 fast_str crate的依赖:

[dependencies]

fast_str = "0.1.0"

使用 fast_str crate中的 FastString 类型创建一个空的字符串:

use fast_str::FastString;

let empty_string = FastString::new();

从字符串字面量创建一个 FastString 对象:

use fast_str::FastString;

let hello_string = FastString::from("Hello");

将一个字符串追加到现有的 FastString 对象:

use fast_str::FastString;

let mut name = FastString::from("John");

name.push_str(" Doe");

将 FastString 转换为 &str 切片:

use fast_str::FastString;

let name = FastString::from("John");

let name_slice: &str = &name;

连接两个 FastString 对象:

use fast_str::FastString;

let first_name = FastString::from("John");

let last_name = FastString::from("Doe");

let full_name = first_name + " " + &last_name;

重复一个 FastString 对象多次:

use fast_str::FastString;

let repeated_string = FastString::from("abc").repeat(3);

检查一个 FastString 是否包含子字符串:

use fast_str::FastString;

let message = FastString::from("Hello, World!");

let contains_hello = message.contains("Hello");

将数字值转换为 FastString :

use fast_str::FastString;

let number = 42;

let number_string = FastString::from(number);

根据分隔符将 FastString 拆分为子字符串:

use fast_str::FastString;

let message = FastString::from("Hello, World!");

let words: Vec<&str> = message.split(", ").collect();

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

精彩链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: