如何在Rust中合并包含特定符号的Vec<>元素

huangapple go评论117阅读模式
英文:

how to combine Vec<> elements containing certain symbols in Rust

问题

以下是您提供的代码的翻译部分:

  1. struct Command {
  2. keyword: String,
  3. args: Vec<String>,
  4. }
  5. fn main() {
  6. let mut c: String = String::from("test \"one two\" three");
  7. let cmd: Vec<String> = c.split_whitespace().map(|s| s.to_string()).collect();
  8. let command: Command = search_quotes(cmd);
  9. }
  10. fn search_quotes(c: Vec<String>) -> Command {
  11. // 这里我需要将引号括起来的元素合并为一个元素。
  12. // 在下面的示例中处理数据以输出
  13. Command {
  14. keyword: "test".to_string(),
  15. args: vec!["\"one two\"".to_string(), "three".to_string()],
  16. }
  17. }

以下是您第二个代码片段的翻译:

  1. let cmd = vec!["test", "\"one", "two\"", "three"];
  2. let mut n: Vec<usize> = vec![];
  3. for i in 0..cmd.len() {
  4. let bytes = cmd[i+1].as_bytes();
  5. for (j, &item) in bytes.iter().enumerate() {
  6. if item == b'"' {
  7. if j == 0 || j == bytes.len()-1 {
  8. n.push(i+1);
  9. }
  10. }
  11. }
  12. }

请注意,我已经将 HTML 实体(如 &quot;)翻译为相应的字符。如果您需要进一步的帮助或有其他问题,请随时提问。

英文:

In general, I need all this to process quotes in entering into Shell, for example, echo &quot;Hello World&quot;, as well as so that the elements in the massif be perceived as a single element.

  1. struct Command {
  2. keyword: String,
  3. args: Vec&lt;String&gt;,
  4. }
  5. fn main() {
  6. let mut c: String = String::from(&quot;test \&quot;one two\&quot; three&quot;);
  7. let cmd: Vec&lt;String&gt; = c.split_whitespace().map(|s| s.to_string()).collect();
  8. let command: Command = search_quotes(cmd);
  9. }
  10. fn search_quotes(c: Vec&lt;String&gt;) -&gt; Command {
  11. // Here I need the elements enclosed in quotes to unite in one element.
  12. // Prosice data for the withdrawal in this example below
  13. Command {
  14. keyword: &quot;test&quot;,
  15. args: [&quot;\&quot;one two\&quot;&quot;, &quot;three&quot;]
  16. }
  17. }

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

  1. let cmd = vec![&quot;test&quot;, &quot;\&quot;one&quot;, &quot;two\&quot;&quot;, &quot;three&quot;];
  2. let mut n: Vec&lt;usize&gt; = vec![];
  3. for i in 0..cmd.len() {
  4. let bytes = cmd[i+1].as_bytes();
  5. for (j, &amp;item) in bytes.iter().enumerate() {
  6. if item == b&#39;&quot;&#39; {
  7. if j == 0 || j == bytes.len()-1 {
  8. let n = n.push(i+1);
  9. }
  10. }
  11. }
  12. }

答案1

得分: 0

以下是翻译好的代码部分:

  1. fn search_quotes(mut c: Vec<String>) -> Command {
  2. let mut i = 0;
  3. let mut has_quote = false;
  4. while i < c.len() {
  5. if has_quote {
  6. let s = c.remove(i);
  7. let s = if s.ends_with('"') {
  8. has_quote = false;
  9. &s[..s.len() - 1]
  10. } else {
  11. &s
  12. };
  13. c[i - 1].push(' ');
  14. c[i - 1].push_str(s);
  15. } else {
  16. if c[i].starts_with('"') {
  17. c[i].replace_range(..1, "");
  18. if c[i].ends_with('"') {
  19. let quote = c[i].len() - 1;
  20. c[i].replace_range(quote.., "");
  21. } else {
  22. has_quote = true;
  23. }
  24. }
  25. i += 1;
  26. }
  27. }
  28. if has_quote {
  29. panic!("unmatched quote");
  30. }
  31. Command {
  32. keyword: c.remove(0),
  33. args: c,
  34. }
  35. }

希望这能帮助您!

英文:

Here's some code that works:

  1. fn search_quotes(mut c: Vec&lt;String&gt;) -&gt; Command {
  2. let mut i = 0;
  3. let mut has_quote = false;
  4. while i &lt; c.len() {
  5. if has_quote {
  6. let s = c.remove(i);
  7. let s = if s.ends_with(&#39;&quot;&#39;) {
  8. has_quote = false;
  9. &amp;s[..s.len() - 1]
  10. } else {
  11. &amp;s
  12. };
  13. c[i - 1].push(&#39; &#39;);
  14. c[i - 1].push_str(s);
  15. } else {
  16. if c[i].starts_with(&#39;&quot;&#39;) {
  17. c[i].replace_range(..1, &quot;&quot;);
  18. if c[i].ends_with(&#39;&quot;&#39;) {
  19. let quote = c[i].len() - 1;
  20. c[i].replace_range(quote.., &quot;&quot;);
  21. } else {
  22. has_quote = true;
  23. }
  24. }
  25. i += 1;
  26. }
  27. }
  28. if has_quote {
  29. panic!(&quot;unmatched quote&quot;);
  30. }
  31. Command {
  32. keyword: c.remove(0),
  33. args: c,
  34. }
  35. }

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.

huangapple
  • 本文由 发表于 2023年7月17日 18:02:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76703360.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定