0

So I have something like this:

let v = vec![...];
let s = Data { vec: &v, ... };

Perhaps this is misguided, but the idea is that many data structures could share the same vector. But for the case where I don't want to share, it would be convenient to have something like this:

let (v, s) = make_data(...);

Apparently, unlike the first example, there is no way to connect the lifetime of v and s (correct me if I'm wrong). Anyway, I understand the borrow checker rejects this. So I end up doing:

let v = vec![];
let s = make_data(&v, ...);

Now, perhaps, I could make one of those work:

let (v, s) = make_data!(...);
let s = make_data!(v, ...);
let s = make_data!(...);

The problem here is that thse macros would expand to something like { let v = vec![]; ... } and v's destructor will be run in the end of this block, but what I really want is to have it expand to something like the first example.

Now, I can make this work:

make_data!(v, s, ...);

But it's odd. Is there any other way to solve this?

  • Why do you want to return a vec and a reference to that vec from the same function? It's trivial to take a reference to that vec after it has been returned. – A.B. Jun 28 '15 at 18:44
  • Oh, I'm not sure I choose the right title. I don't actually want to return (Vec<T>, &[T]). The reference is embedded inside a data structure. I can, of course, first create the vector, then create the structure, like shown in the first and third examples -- but in some cases I want to create the two with a single statement. – darque Jun 28 '15 at 19:03
1

Rust allows you to define multiple variables with the same name in the same block.

let a = vec![...];
let a = Data { vec: &a, ... };

On the second line, the new a is not in scope yet, so you can still refer to the previous definition of a. However, on the following statements, you can no longer refer to the original a definition, since the second definition shadows the first one; nevertheless, the Vec remains alive until the end of the block, as usual.

You can take advantage of this in your macro by only taking a single identifier and using it for both the Vec and the slice.

  • That's a neat trick, thanks. I ended up moving away from this design, it's simpler to have Data own its vector. – darque Jun 30 '15 at 0:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.