Prototype programming like in Rust

Just a thought came to my mind, like to hear feedbacks/comments about it.
Is there any pro/con (simplifications or more difficulties) could be generated of building a rust app in a way close to prototype programming, something like:

// Defining structs to be used
#[derive(Debug, Clone, Default)]
struct NAME<'a> {first: &'a str, last: &'a str}

Impl<`a> NAME<`a> {
.
.
}

// Define types aliases
type Name<'a> = NAME<'a>;

// Define `App` struct to hold all the variables and structs of the program 
#[derive(Debug, Clone, Default)]
struct App<'a>{
    age: u32,
    name: Name<'a>
}

// Implementation of the `App` including all functions to be used
impl<'a> App<'a> {
    fn build(&mut self) {
        self.age = 5;
        self.name.first = "Karam";
        self.name.last = "Yousef";
        println!("{:?}", self);
    }
    .
    .
   
}

// Define. build and run the `App` from the `main`
fn main(){
    let mut app = App::default();
    app.build();
}

This won't work due to lifetimes. When you create the default instance, it decides what lifetime its temporarily borrowed field have. It will not accept any values that were created later, because everything created later will be in a smaller scope.

Your example happens to work only because string literals are special and basically ignore lifetimes. It would be a real torture to work with such struct if you needed to store and update actual non-constant strings there.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.