1
submitted 2 weeks ago* (last edited 2 weeks ago) by dontblink@feddit.it to c/rust@lemmy.ml

Let's say I have a struct Event which implements the method permalink:

struct Event {
    base_url: String,
    rel_permalink: String,
    permalink: String,
    title: String,
}

impl Event {
    fn permalink(&self) -> String {
        let permalink = format!("{}{}", self.base_url, self.rel_permalink);
        permalink
    }
}

The method takes 2 fields of the struct and the target would be to return the definition of another field.

Later I instantiate an event1: Event:

let event1 = Event {
                base_url: base_url,
                rel_permalink: rel_permalink.to_string(),
                title: node.value().name().to_string(),
                permalink = permalink(),
            };

Essentially I would like the field permalink to be the value returned by the method permalink, is something like this possible, is this correct? I couldn't find something similar in the docs..

Pheraps using an associated function as constructor would be a better way to handle a similar situation?

Thank you so much!

no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here
this post was submitted on 15 Nov 2024
1 points (100.0% liked)

Rust Programming

8197 readers
1 users here now

founded 5 years ago
MODERATORS