rust_programming_book 0.1.1

Programming works from THE RUST PROGRAMMING LANGUAGE
Documentation
#[allow(unused_imports)]
use crate::traits::SomeTraits;
#[allow(unused_imports)]
use rand::Rng;
#[allow(unused_imports)]
use std::cmp::Ordering;
#[allow(unused_imports)]
use std::io;
mod collections;
mod control_flow;
mod enums;
mod error_handling;
mod generics;
mod impl_dyn;
mod implementation;
mod lifetimes;
mod ownership;
mod polymorphism;
mod structs;
mod traits;
use rust_programming_book::closure;
#[allow(dead_code, unused_variables)]
fn main() {
    // guessing_game();

    // const ORIGINAL_VLAUE: i32 = 50;
    // let loop_value = make_loop(&ORIGINAL_VLAUE);
    // println!("after looping: {loop_value}");

    // ownership::ownership();

    // ownership::slice();

    // structs::create_user();
    // lifetimes::lifetimes();

    // let el1 = implementation::Elephant::create_elephant(10., "Puspa");
    // let el2 = implementation::Elephant::create_elephant(12.2, "Anjali");

    // println!("El1: {:#?}\nEl2: {:#?}", el1, el2);

    // println!(
    //     "{} is bigger than {}: {:?}",
    //     el1.name,
    //     el2.name,
    //     el1.compare_elephant(&el2)
    // );

    // let cow1 = traits::Cow {
    //     name: "Chauri Gai",
    //     age: 20,
    //     height: 3.2,
    // };

    // // we access the method which doesn't have self parameter using dot operator and using :: for another type
    // println!("Elephant {} is lovely: {:?}", el1.name, el1.is_lovely());
    // println!("Cow are lovely: {:?}", cow1.is_lovely());

    // println!(
    //     "Elephant {} is big enough: {:?}",
    //     el2.name,
    //     el2.is_big_enough()
    // );
    // println!("{} is big enough: {:?}", cow1.name, cow1.is_big_enough());

    /*
    enums and pattern matching
    */

    // enums::enum_display();

    // enums::pattern_matching();

    /*
    polymorphism(using and returning traits)
    */

    // polymorphism::polymorphism();
    // polymorphism::payment();

    /*
    control flow
    */
    // control_flow::control_flow();
    // control_flow::concise_control_flow();

    /*
    generics
    */
    // generics::generics_for_vector();

    /*
    dyn vs impl
    */
    // impl_dyn::impl_dyn();

    /*
    collections
    */
    // collections::collection();

    /*
    error handling
    */
    // error_handling::error_handling();

    /*
    closures
    */

    // closure::capturing_reference();
    closure::fn_mut_example();
}

// fn guessing_game() {
//     println!("Guess the number!");

//     let secret_number = rand::thread_rng().gen_range(1..=100);

//     loop {
//         println!("Please input your guess:");

//         let mut guess = String::new();

//         io::stdin()
//             .read_line(&mut guess)
//             .expect("Failed to read line");

//         let guess: u32 = match guess.trim().parse() {
//             Ok(num) => num,
//             Err(_) => continue,
//         };

//         println!("Your guess is : {guess}");

//         match guess.cmp(&secret_number) {
//             Ordering::Less => println!("Too small!"),
//             Ordering::Equal => {
//                 print!("YOU WIN!");
//                 break;
//             }
//             Ordering::Greater => println!("Too big!"),
//         }
//     }
// }

// fn make_loop(value: &i32) -> i32 {
//     let mut counter = 0;
//     let loop_value = loop {
//         counter += 1;

//         if counter == 10 {
//             break counter * value;
//         }
//     };

//     loop_value
// }

// fn calculate_length(string: &String) -> usize {
//     return string.len();
// }