Is it a good idea to use a lot of borrowed data from the start?

Hi.
I'm rewriting some C++ codes with Rust.

It's early days, and I don't know everything about how the original C++ code works.

Currently, whenever I define a struct, I use borrowed type first.
If I find I can't use borrowed type, I change it to an owned type later. Is this okay?

Or should I define most of them as owned type and change to borrowed type later?

For struct fields, you want owned types most of the time. It's not even a question of changing it to borrowed later.

But for function fields, you should prefer borrowed types. Owned types are only needed here when the function needs ownership.

10 Likes

I don't know everything about how the original C++ code works

Whatever strategy for ownership in Rust code you will choose, this will come later to bite you. I would strongly suggest to firstly have a thorough understanding of C++ architecture, before you start rewriting it.

As to your main question, I would also suggest to start with owned types and clone, or use reference counting when necessary, and not start with borrowing and lifetimes. Lifetimes are a leaky abstraction, and you can quickly run into deep problems, when you don't know from the start what you want to do. And since you said that you don't understand architecture very well, the risk of choosing wrong borrowing model is even greater.

That is all I can tell as a general advice. If you want something more concrete, you should show us some code.

6 Likes

Thank you all

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.