Replying to Avatar Dan Lyke

nostr:nprofile1qy2hwumn8ghj7un9d3shjtnddaehgu3wwp6kyqpq4cgq353exzmhdsvqjtmw4dq7fvyleuls8umyrvd5umhr4gtx6asqzk33ng I'm fumbling around a lot with lifetime specifications, and for how often they're needed, the syntax for them seems pretty danged clunky.

I think I want a way to guarantee that this "Chars" points to the same buffer as another "Chars", and I'm doing that by giving them the same lifetime as the object which is processing them, which lets me compile, but feels like I'm not getting this right.

The apostrophe needed in the syntax is confusing to me (...<'a>...)

You can also share buffers by having the two "Chars" point to the same buffer, like so below

-----

struct Chars<'a> {

data: &'a str,

}

fn process_chars<'a>(chars1: Chars<'a>, chars2: Chars<'a>) {

// Both chars1 and chars2 point to the same buffer

println!("Processing: {} and {}", chars1.data, chars2.data);

}

-----

I'm really not good at any of this so please take this with less than two sats of my own worth here. Trying to help both yourself & my own C/C++ to Rust journey 💜

I was also told to pay better attention to the borrow checker's messages too

Reply to this note

Please Login to reply.

Discussion

nostr:nprofile1qy2hwumn8ghj7un9d3shjtnddaehgu3wwp6kyqpq4cgq353exzmhdsvqjtmw4dq7fvyleuls8umyrvd5umhr4gtx6asqzk33ng yeah, I'm doing parse nodes, and have a `fn matches<'a>(buffer : &Chars<'a>) -> (bool, Chars<'a>);`, and I hope this means that the second return value is in the same `str` referenced by `buffer`.

But I'm totally not sure. And it doesn't work if I pull the <'a> after "matches".

I do know that the placement of the lifetime parameter is imprtsmt. Gotta define a function with lifetimes and be specified before the parameters . its something to do with the complier has to know how lifetimes relate to each other when function sigs are checked.

< 'a > after 'matches' will confuse the complier to associate the lifetime with the parameters.