[rust-dev] Help needed with lifetimes.. (I'm new to rust)

Clark Gaebel cg.wowus.cg at gmail.com
Mon Oct 20 13:02:46 PDT 2014


`clean_number` inside the function `clean` is destructed, and a slice into the destructed String is returned. You’re probably looking for the `MaybeOwned` enum in the standard library to do what you want:





http://is.gd/QvkESn




Regards,

  - Clark

On Mon, Oct 20, 2014 at 12:57 PM, Jake Scott <jake.net at gmail.com> wrote:

> Can someone give me a hand, I'm getting the following lifetime error:
> error: clean_number does not live long enough
> Short url: http://is.gd/VIzHMS
> This is the code:
> #![feature(phase)]
> #[phase(plugin)]
> extern crate regex_macros;
> extern crate regex;
> use regex::Regex;
> static ALPHA_REGEX: Regex = regex!(r"[a-zA-Z]");
> static NUMBER_REGEX: Regex = regex!(r"[^0-9]");
> fn main() {
>     let number = number("ads(123) 456-7890");
>     println!("{}", number);
> }
> fn number<'a>(number: &'a str) -> &'a str {
>     let clean_number = clean(number);
>     clean_number
> }
> fn clean<'a>(number: &'a str) -> &'a str {
>     if ALPHA_REGEX.is_match(number) {
>         "0000000000";
>     }
>     let clean_number: String = NUMBER_REGEX.replace(number, "");
>     return clean_number.as_slice();
> }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20141020/b5a11e68/attachment.html>


More information about the Rust-dev mailing list