#Using a String in a struct

1 messages · Page 1 of 1 (latest)

true pier
#

I am literally just getting started with RUST, and the best way for me is to get myhands dirty so I am trying to experiment by building random stuff, reading, copying code, and just messing around, but I have hit a snag that I dont seem to really understand whats going on.

take this:

struct context<'a> {
    custom: &'a mut custom,
}

struct custom<'a> {
    search: &'a String,
}

Somewhere else I have this:

fn process(
    data: &mut custom,
){
   // How can I change the search string value inside custom?
  data.search =  "bananas"
}

Another question do I actually need to make custom mut inside context struct, if I what I want to change is just the string and not the actual custom reference?
Am I using lifecycles correctly? I followed that pattern from some other piece of code I saw online, but tbh I still havent fully grasped the concept.

Any pointers/help would be apreciated 🙂

cunning adder
#

Generally structs shouldn't include lifetimes unless they're very short-lived, which is rare. So it should just be search: String