如何避免在比赛中多次调用同一字符串上的".clone()"?



背景:

我有一些代码(Rust)可以找到(Regex)匹配项,并将找到的值分配给名为Articlestruct中的字段(其中所有字段都是String类型):

pub struct Article {
// user facing data
title: String,
category: String,
subcategory: String,
genre: String,
published: String,
estimated_read_time: String,
description: String,
tags: String,
keywords: String,
image: String,
artwork_credit: String,
// meta data
metas: String,
// location
path: String,
slug: String,
// file data
content: String
}

正则表达式("//- define (.*?): (.*?)n")用于从定义文章数据的文章模板中提取注释:

// iterate through HTML property pattern matches
for capture in re_define.captures_iter(&file_content as &str) {
// remove the declaration from the the HTML output
article_content = article_content.replace(&capture[0].to_string(), "");
// get the property value
let property_value: &String = &capture[2].to_string();
// determine what field to assign the property to and assign it
match capture[1].to_lowercase().as_str() {
"title" => article.title = property_value.clone(),
"category" => article.category = property_value.clone(),
"subcategory" => article.subcategory = property_value.clone(),
"genre" => article.genre = property_value.clone(),
"published" => article.published = property_value.clone(),
"estimated_read_time" => article.estimated_read_time = property_value.clone(),
"description" => article.description = property_value.clone(),
"tags" => article.tags = property_value.clone(),
"keywords" => article.keywords = property_value.clone(),
"image" => article.image = property_value.clone(),
unknown_property @ _ => {
println!("Ignoring unknown property: {}", &unknown_property);
}
}
}

注意articleArticle的一个实例。

问题:

代码有效,但我关心的是以下部分:

"title" => article.title = property_value.clone(),
"category" => article.category = property_value.clone(),
"subcategory" => article.subcategory = property_value.clone(),
"genre" => article.genre = property_value.clone(),
"published" => article.published = property_value.clone(),
"estimated_read_time" => article.estimated_read_time = property_value.clone(),
"description" => article.description = property_value.clone(),
"tags" => article.tags = property_value.clone(),
"keywords" => article.keywords = property_value.clone(),
"image" => article.image = property_value.clone(),

它在相同的String(property_value)上为每个匹配(每个文章模板10个匹配)、每个文章模板(总共几十个模板)调用.clone(),我认为这不是最有效的方法

注意:我不确定match是否正在为不匹配进行克隆。

我尝试过的:

我尝试引用property_valueString,但每次引用都有一个错误。

IDE错误(VS代码)

mismatched types
expected struct `std::string::String`, found `&&std::string::String`
expected due to the type of this binding
try using a conversion method: `(`, `).to_string()`

来自cargo check的错误

error[E0308]: mismatched types
--> src/article.rs:84:38
|
84 |           "image" => article.image = &property_value,
|                      -------------   ^^^^^^^^^^^^^^^ expected struct `std::string::String`, found `&&std::string::String`
|                      |
|                      expected due to the type of this binding
|
help: try using a conversion method
|
84 |           "image" => article.image = (&property_value).to_string(),
|                                      +               +++++++++++++

我确实尝试过使用.to_string(),但我不确定将String转换为相同类型是否最有效。

问题:

如何避免在property_value上多次呼叫.clone()

根据类型,您应该只需要在property_value中删除借款,然后就不需要.clone()s。

let property_value: &String = &capture[2].to_string();
// change to
let property_value: String = capture[2].to_string();
// or just simply
let property_value = capture[2].to_string();

我假设这是在capture[2]返回str(非大小类型)时添加的,这将需要&,但使用to_string(),它会转换为自己拥有的类型String,这本身就很好。无论如何,这不会对to_string()副本的性能产生任何影响。

相关内容

  • 没有找到相关文章