C
C
CNNRNN2020-02-05 09:54:57
Rust
CNNRNN, 2020-02-05 09:54:57

Use String or &str?

I want to parse a request from a browser. Let's say I received an array of bytes and I need to structure it somehow.
The question is what is better to use, references to data found in an array or directly own the data.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Demin, 2020-02-13
@keksmen

The solution here is not entirely clear.
On the one hand, it would be much more efficient to drag links (& str), because copying them or extracting the link to the subslice is very cheap. This will allow structures to be created and discarded when parsed without regard to the overhead from owning the String.
On the other hand, by using references, you commit yourself to ensuring that the original string will live longer than your AST. It imposes decent restrictions on your code.
In any case, I would recommend using &str. However, without code, I can’t tell you how best to save the original String until the end of the request processing.

Similar questions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question