gpt4 book ai didi

string - 如何传递修改后的字符串参数?

转载 作者:行者123 更新时间:2023-11-29 08:07:29 24 4
gpt4 key购买 nike

我在 chapter 12 of The Rust Programming Language ,其中实现了不区分大小写的行搜索。两次实现相同的逻辑对我来说没有意义,所以我想如果我只是调用区分大小写的搜索函数并将参数转换为小写,那可能会起作用。它没。

这是我的非工作代码:

fn main() {
let a = search("Waldo", "where in\nthe world\nis Waldo?");
let b = search("waldo", "where in\nthe world\nis Waldo?");
let c = search_case_insensitive("waldo", "where in\nthe world\nis Waldo?");

println!("{:?}", a);
println!("{:?}", b);
println!("{:?}", c);
}

pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
let mut results = Vec::new();

for line in contents.lines() {
if line.contains(query) {
results.push(line);
}
}

results
}

pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
let query = query.to_lowercase();
let contents2: &str = &contents.to_lowercase();

search(&query, contents2)
}

我想出的大多数版本中的错误不可避免地非常类似于:

error[E0597]: borrowed value does not live long enough
--> src/main.rs:25:28
|
25 | let contents2: &str = &contents.to_lowercase();
| ^^^^^^^^^^^^^^^^^^^^^^^ temporary value does not live long enough
...
28 | }
| - temporary value only lives until here
|
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 23:1...
--> src/main.rs:23:1
|
23 | pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

最佳答案

编辑 2:

既然你已经用 MCVE 更新了问题,并且你已经声明你不关心偏离书中的例子......这是另一个依赖于通过使用 String< 进行额外分配的版本:

fn main() {
let a = search("Waldo", "where in\nthe world\nis Waldo?");
let b = search("waldo", "where in\nthe world\nis Waldo?");
let c = search_case_insensitive("waldo", "where in\nthe world\nis Waldo?");

println!("{:?}", a);
println!("{:?}", b);
println!("{:?}", c);
}

pub fn search<S>(query: S, contents: S) -> Vec<String> where S: Into<String> {
let query = query.into();
let mut results = Vec::new();

for line in contents.into().lines() {
if line.contains(&query) {
results.push(line.into());
}
}

results

}

pub fn search_case_insensitive<S>(query: S, contents: S) -> Vec<String> where S: Into<String> {
let query = query.into().to_lowercase();
let contents = contents.into().to_lowercase();

search(query, contents)
}

这是running in the Playground

编辑:

我意识到我从来没有真正给过你一个选择。这是我可能会做的:

pub enum SearchOptions {
CaseSensitive,
CaseInsensitive
}

pub fn search<'a>(query: &str, contents: &'a str, options: SearchOptions) -> Vec<&'a str> {
let mut results = Vec::new();

for line in contents.lines() {
let check = match options {
SearchOptions::CaseSensitive => line.contains(query),
SearchOptions::CaseInsensitive => line.to_lowercase().contains(&query.to_lowercase()),
};

if check {
results.push(line);
}
}

results
}

这就是您可以对其进行“去重”的最大程度。

原答案:

实际的问题是当 contents 绑定(bind)到生命周期 'a 时,你试图传递它......但你真正想成为什么“不区分大小写”是 query

这并不以完全相同的方式绑定(bind)到生命周期 'a,因此......有效:

pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
let query = query.to_lowercase();
search(&query, contents)
}

Here it is on the playground

虽然你仍然需要复制逻辑......因为你需要将小写查询与小写行相匹配......这在书中的示例中得到了证明:

if line.to_lowercase().contains(&query) {
// ^^^^^^^^^^^^^^ each LINE is converted to lowercase here in the insensitive search
results.push(line);
}

“如何停止重复逻辑?” - 嗯,他们一开始就不完全一样。我认为您的尝试并不是您最初想要的(不过很高兴得到纠正)。

关于string - 如何传递修改后的字符串参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51166835/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com