Hello everybody,

I am to new to this list (and to the very promising language that Rust 
definitely is).

While learning Rust and writing my first applications in it I stumbled 
upon the problem of matching an owned box to a variable pattern. I am 
wondering if there is any syntax to convert an owning pointer to a 
borrowed one when binding to a variable pattern.

Please consider the following enum:

enum Foobar {
     Foo(~str),
     Bar(~str)
}

Now, I would like to write a function

fn f<'a>(fb: &'a Foobar) -> &'a str

that takes a borrowed pointer to Foobar and returns a borrowed pointer 
to a string from either Foo or Bar. I know I can write it this way:

fn f<'a>(fb: &'a Foobar) -> &'a str {
     let rs = match *fb {
         Foo(ref rs) => rs,
         Bar(ref rs) => rs
     };
     let s : &'a str = *rs;
     s
}

However, is there an alternative that does not introduce (ref) 
indirection? Something that would make s in the code below be of type 
&str instead of ~str?

fn f<'a>(fb: &'a Foobar) -> &'a str {
     match *fb {
         Foo(s) => s,
         Bar(s) => s
     }
}

Wojciech
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to