英文:
how to combine Vec<> elements containing certain symbols in Rust
问题
以下是您提供的代码的翻译部分:
struct Command {
keyword: String,
args: Vec<String>,
}
fn main() {
let mut c: String = String::from("test \"one two\" three");
let cmd: Vec<String> = c.split_whitespace().map(|s| s.to_string()).collect();
let command: Command = search_quotes(cmd);
}
fn search_quotes(c: Vec<String>) -> Command {
// 这里我需要将引号括起来的元素合并为一个元素。
// 在下面的示例中处理数据以输出
Command {
keyword: "test".to_string(),
args: vec!["\"one two\"".to_string(), "three".to_string()],
}
}
以下是您第二个代码片段的翻译:
let cmd = vec!["test", "\"one", "two\"", "three"];
let mut n: Vec<usize> = vec![];
for i in 0..cmd.len() {
let bytes = cmd[i+1].as_bytes();
for (j, &item) in bytes.iter().enumerate() {
if item == b'"' {
if j == 0 || j == bytes.len()-1 {
n.push(i+1);
}
}
}
}
请注意,我已经将 HTML 实体(如 "
)翻译为相应的字符。如果您需要进一步的帮助或有其他问题,请随时提问。
英文:
In general, I need all this to process quotes in entering into Shell, for example, echo "Hello World"
, as well as so that the elements in the massif be perceived as a single element.
struct Command {
keyword: String,
args: Vec<String>,
}
fn main() {
let mut c: String = String::from("test \"one two\" three");
let cmd: Vec<String> = c.split_whitespace().map(|s| s.to_string()).collect();
let command: Command = search_quotes(cmd);
}
fn search_quotes(c: Vec<String>) -> Command {
// Here I need the elements enclosed in quotes to unite in one element.
// Prosice data for the withdrawal in this example below
Command {
keyword: "test",
args: ["\"one two\"", "three"]
}
}
Here is one of my attempts, here I wanted to fix the elements with quotes at the beginning and end by adding their place to the variable, and then I would just be able to fold all the elements in one
let cmd = vec!["test", "\"one", "two\"", "three"];
let mut n: Vec<usize> = vec![];
for i in 0..cmd.len() {
let bytes = cmd[i+1].as_bytes();
for (j, &item) in bytes.iter().enumerate() {
if item == b'"' {
if j == 0 || j == bytes.len()-1 {
let n = n.push(i+1);
}
}
}
}
答案1
得分: 0
以下是翻译好的代码部分:
fn search_quotes(mut c: Vec<String>) -> Command {
let mut i = 0;
let mut has_quote = false;
while i < c.len() {
if has_quote {
let s = c.remove(i);
let s = if s.ends_with('"') {
has_quote = false;
&s[..s.len() - 1]
} else {
&s
};
c[i - 1].push(' ');
c[i - 1].push_str(s);
} else {
if c[i].starts_with('"') {
c[i].replace_range(..1, "");
if c[i].ends_with('"') {
let quote = c[i].len() - 1;
c[i].replace_range(quote.., "");
} else {
has_quote = true;
}
}
i += 1;
}
}
if has_quote {
panic!("unmatched quote");
}
Command {
keyword: c.remove(0),
args: c,
}
}
希望这能帮助您!
英文:
Here's some code that works:
fn search_quotes(mut c: Vec<String>) -> Command {
let mut i = 0;
let mut has_quote = false;
while i < c.len() {
if has_quote {
let s = c.remove(i);
let s = if s.ends_with('"') {
has_quote = false;
&s[..s.len() - 1]
} else {
&s
};
c[i - 1].push(' ');
c[i - 1].push_str(s);
} else {
if c[i].starts_with('"') {
c[i].replace_range(..1, "");
if c[i].ends_with('"') {
let quote = c[i].len() - 1;
c[i].replace_range(quote.., "");
} else {
has_quote = true;
}
}
i += 1;
}
}
if has_quote {
panic!("unmatched quote");
}
Command {
keyword: c.remove(0),
args: c,
}
}
It is not the more performant neither most idiomatic (for example, you probably don't want to panic on invalid input), but it demonstrates the idea.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论