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

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

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 实体(如 &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.

struct Command {
    keyword: String,
    args: Vec&lt;String&gt;,
}

fn main() {
    let mut c: String = String::from(&quot;test \&quot;one two\&quot; three&quot;);
    let cmd: Vec&lt;String&gt; = c.split_whitespace().map(|s| s.to_string()).collect();
    
    let command: Command = search_quotes(cmd);
}

fn search_quotes(c: Vec&lt;String&gt;) -&gt; 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: &quot;test&quot;,
        args: [&quot;\&quot;one two\&quot;&quot;, &quot;three&quot;]
    }
}

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![&quot;test&quot;, &quot;\&quot;one&quot;, &quot;two\&quot;&quot;, &quot;three&quot;];
let mut n: Vec&lt;usize&gt; = vec![];
    for i in 0..cmd.len() {
        let bytes = cmd[i+1].as_bytes();
        for (j, &amp;item) in bytes.iter().enumerate() {
            if item == b&#39;&quot;&#39; {
                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&lt;String&gt;) -&gt; Command {
    let mut i = 0;
    let mut has_quote = false;
    while i &lt; c.len() {
        if has_quote {
            let s = c.remove(i);
            let s = if s.ends_with(&#39;&quot;&#39;) {
                has_quote = false;
                &amp;s[..s.len() - 1]
            } else {
                &amp;s
            };
            c[i - 1].push(&#39; &#39;);
            c[i - 1].push_str(s);
        } else {
            if c[i].starts_with(&#39;&quot;&#39;) {
                c[i].replace_range(..1, &quot;&quot;);
                if c[i].ends_with(&#39;&quot;&#39;) {
                    let quote = c[i].len() - 1;
                    c[i].replace_range(quote.., &quot;&quot;);
                } else {
                    has_quote = true;
                }
            }
            
            i += 1;
        }
    }
    
    if has_quote {
        panic!(&quot;unmatched quote&quot;);
    }
    
    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.

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

发表评论

匿名网友

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

确定