Thank you, Micah.  Your suggestion and Léo Testard's suggestion using
s.as_slice() (emailed off-list) work well.

Cadence


On Tue, Nov 12, 2013 at 8:01 AM, Micah Chalmer <mi...@micahchalmer.net>wrote:

> This will work for your pattern match without cloning:
>
>         match self.opt_err {
>             None => format!("compilation failed at offset {:u}",
> self.erroffset as uint),
>             Some(ref s) => format!("compilation failed at offset {:u}:
> {:s}", self.erroffset as uint, *s)
>         }
>
> It takes a reference to the string instead of moving it.  You end up with
> a pointer to a pointer (&~) which is why you have to explicitly dereference
> it when you use it.
>
> -Micah
>
> On Nov 12, 2013, at 7:41 AM, Cadence Marseille <cadencemarsei...@gmail.com>
> wrote:
>
> Hi,
>
> I was thinking about a small enhancement for Option<S> objects where S:
> Str that would add a utility method for returning a slice option.
>
> There is some code in rust-pcre,
> https://github.com/cadencemarseille/rust-pcre/blob/d833054/src/pcre/mod.rs#L137,
> that I think illustrates what I am trying to achieve.  I want to use
> pattern matching, but both of these versions fail to compile:
>
>         match self.opt_err {
>             None => format!("compilation failed at offset {:u}",
> self.erroffset as uint),
>             Some(s) => format!("compilation failed at offset {:u}: {:s}",
> self.erroffset as uint, s)
>         }
>
> (error: cannot move out of dereference of & pointer).  Makes sense.  This
> is fixed by clone()ing self.opt_err.
>
>         match self.opt_err.clone().map(|s| s.as_slice()) {
>             None => format!("compilation failed at offset {:u}",
> self.erroffset as uint),
>             Some(s) => format!("compilation failed at offset {:u}: {:s}",
> self.erroffset as uint, s)
>         }
>
> (error: borrowed value does not live long enough).  Also makes sense.
>
> I want to avoid the clone(), so I thought that maybe an additional utility
> method is needed to return a slice option given an Option<S: Str>.
>
> I have tried this idea out with:
>
> use std::option::Option;
>
> trait OptionStr {
>     fn as_slice<'a>(&'a self) -> Option<&'a str>;
> }
>
> impl<S: Str> OptionStr for Option<S> {
>     #[inline]
>     fn as_slice<'a>(&'a self) -> Option<&'a str> {
>         match *self {
>             Some(ref s) => Some(s.as_slice()),
>             None => None
>         }
>     }
> }
>
> fn main() {
>     let opt_str = Some(~"hello world!");
>     match opt_str.as_slice() {
>         Some(s) => println(s),
>         None => return
>     }
> }
>
> I don't think that the OptionStr would need an into_owned() method (like
> Str.into_owned()<http://static.rust-lang.org/doc/master/std/str/trait.Str.html#tymethod.into_owned>)
> because that can be done with map: opt_str.map(|s| s.into_owned()).
>
> Does this sound like a good idea?  I can put together a Pull Request if
> there is interest in this.
>
> Cadence
>  _______________________________________________
> Rust-dev mailing list
> Rust-dev@mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>
>
>
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to