rust_programming_book 0.1.1

Programming works from THE RUST PROGRAMMING LANGUAGE
Documentation
/**

we need lifetime when the input to function has multiple references and output also has
reference. When there is single input reference and single output reference we don't
need to explicitly write the life time, compiler automatically does for us.

Similar case for struct.

 */
#[allow(dead_code, unused_features)]
#[derive(Debug)]
struct User<'a> {
    active: bool,
    username: &'a str,
    email: &'a str,
    sign_in_count: u64,
}

#[allow(dead_code)]
pub fn lifetimes() {
    let user1 = User {
        email: "someone@example.com",
        username: "someusername123",
        active: true,
        sign_in_count: 1,
    };

    println!("{:#?}", user1);

    let user2 = User {
        email: "pokhrelsuraj1211@gmail.com",
        username: "surajpokhrel",
        active: true,
        sign_in_count: 2,
    };

    let bigger_email = find_greater(&user1.email, &user2.email);
    println!(
        "user1: {:#?}\n user2: {:#?}\n bigger email: {:?}",
        user1, user2, bigger_email
    );
}

#[allow(dead_code)]
/// finds the bigger string
pub fn find_greater<'a>(str1: &'a str, str2: &'a str) -> &'a str {
    if str1.len() > str2.len() {
        str1
    } else {
        str2
    }
}