Struct with String and &str

Hi, I would like to create a struct:

pub struct Request {
    // ...
    protocol: &str,
    request_path: &str,
    raw: String,
    // ...
}

Where raw field would contain a raw input string and fields protocol and request_path would contain a slices of a raw field, but I am struggling to tell the compiler what I intend. Is it even possible and if yes how?

Thanks in advance!

This is not possible. A struct where one field has a reference to another field of the same struct is called self-referential, and these are not possible in safe Rust. You could store a Range<usize> and look up in raw when you want the data.

7 Likes

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.