cargo.toml:
...
[dependencies]
colored="1.7.1"
main.rs:
use colored::*;
fn main() {
let text = "Rust是一门安全的语言".to_string();
println!("{}", text);
println!(
"{}~{}之间的内容是{}",
"Rust".yellow(),
"语言".yellow(),
text.between("Rust", "语言").bright_green()
);
}
trait Between {
fn between(&self, first_key: &str, last_key: &str) -> &str;
}
impl Between for String {
fn between(&self, first_key: &str, last_key: &str) -> &str {
if let Some(index1) = self.find(first_key) {
if let Some(index2) = self.find(last_key) {
return self
.split_at(index1 + first_key.len())
.1
.split_at(index2 - index1 - first_key.len())
.0;
}
}
""
}
}
1
共 1 条评论, 1 页
评论区
写评论好用,之前看过。