I'm new to rust and was playing around with the code below. When I do the match on data.s1 it is moved. How to do a borrow instead, so the the following println! calls will compile? It looks like the following println! calls do a move also. I can't seem to find any info on this case. Thanks -S
BTW: Sorry, I couldn't figure out how to quote the code. I searched Google and the forum to no avail.
let sval = match data.s1 {
Some(ref s) => s,
None => "None"
};
The ref modifier makes the binding take a reference to the inside of the matched pattern.
(Note that in order for this to work, you should also change the type of the string "None" from owned to borrowed, so I omitted the .to_string() part.)
I have serious concerns about the downsides of default binding modes (that I'm not going to reiterate here), therefore I'm suggesting the clearer, explicit alternative.
The ref annotation works, but I get a compile error on &data.s1 "match arm incompatible type" for None. I guess this is because the & is turning the whole option to a reference as opposed to the Some(s). I'm not sure how to dereference the None though. I'll have to play around with it some more.
I don't know what's your exact code but I guess you still have "None".to_string(). The problem is that in your example you try to return an owned String while the match &data.s1 {} solution returns a reference, i.e. &String. If the .to_string() is omitted you return "None" directly which is of type &'static str like every "String" literal in Rust. To prevent confusion: In this context &'static str will be cast into &String automaticlly. see what @RustyYato wrote.