How do I get a substring between two patterns in Rust?
<p>I want to create a substring in Rust. It starts with an occurrence of a string and ends at the end of the string minus four characters or at a certain character.</p>

<p>My first approach was </p>

<pre><code>string[string.find("pattern").unwrap()..string.len()-5]
</code></pre>

<p>That is wrong because Rust's strings are valid UTF-8 and thus byte and not char based.</p>

<p>My second approach is correct but too verbose:</p>

<pre><code>   let start_bytes = string.find("pattern").unwrap();
   let mut char_byte_counter = 0;
   let result = line.chars()
    .skip_while(|c| {
        char_byte_counter += c.len_utf8();
        return start_bytes &gt; char_byte_counter;
    })
    .take_while(|c| *c != '&lt;')
    .collect::&lt;String&gt;();
</code></pre>

<p>Are there simpler ways to create substrings? Is there any part of the standard library I did not find?</p>