0

I have a struct that contains a string.

struct Foo {
    s: String
}

This string gets extended with bytes that I want to pass out of Foo and give to some other code.

I can achieve this with a copy

other.s = self.s.clone();
self.s.clear();

but it jars because its clearly a waste and s might be kilobytes.

I need Foo to own the data, temporarily, passing pointer introduces lifetime issues I don't want to have to deal with. So I would like to pass ownership of the underlying bytes.

I can also do this with an s: Option<String> and s.take() , temporarily s can be null but the syntax for manipulating data in an Option is nasty, and I never really want s to be null.

Peter Hall
  • 53,120
  • 14
  • 139
  • 204
teknopaul
  • 6,505
  • 2
  • 30
  • 24
  • 1
    I think this [anwser](https://stackoverflow.com/a/41370552/865874) applies to your question too. – rodrigo Oct 08 '20 at 09:24
  • 2
    The other answer applied to your question: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=533b5da32ea5d9549774830aecbf4066 – Peter Hall Oct 08 '20 at 10:31

0 Answers0