• 22 Posts
  • 60 Comments
Joined 11 months ago
cake
Cake day: June 4th, 2025

help-circle

  • Welcome to Rust, I hope you enjoyed learning and using it :)

    One major advice would be to run cargo clippy, which gives a lot of helpful hints. Apart from that I also have some feedback.

        40	struct VendorOuiData {
        41	    vendor_organization: String, // Vendor Organization
    

    These comments should be doc-comments (///) so that cargo doc picks them up.

        49	fn fetch_oui_database(url_string: &String) -> Result<String, Box<dyn std::error::Error>> {
    

    Not saying that your return type is bad, but in general you might be interested in crates thiserror and anyhow that simplify error handling.

        51	        .timeout(Duration::new(60, 0))
    

    Duration::from_mins(1) would be much more readable.

        66	                panic!("{}", err);
    

    You should almost never panic. Your function already returns a Result<>, so you should be using that for error handling.

        70	    } else {
        71	        // HTTP Status is not 200
    

    Not Rust specific, but usually I recommend to handle errors first, and return from the function early. This way you reduce cognitive load on the reader, and reduce nesting of if-blocks.

       150	                let mut parts = line.splitn(2, '\t');
    

    There’s a convenient method to split in two substrings that you could have used:

    let (mac_address, vendor_organization) = line.split_once('\t').unwrap_or(("", ""));
    
       166	                vendor_oui_data.vendor_address += line
       167	                    .trim()
       168	                    .split(' ')
       169	                    .filter(|s| !s.is_empty())
       170	                    .collect::<Vec<_>>()
       171	                    .join(" ")
       172	                    .as_str();
    

    I would probably write a regular expression that replaces all contiguous whitespace with a single space instead.

       173	                vendor_oui_data.vendor_address.push('\n');
    

    Aren’t you trimming this new line off in line 181?

       181	        vendor_oui_data.vendor_address = vendor_oui_data.vendor_address.trim().to_string();
    

    This is somewhat inefficient, since you’ll be allocating and copying your string here.










  • I’ll cover item 1: start by writing a document, describing what you want to achieve: what do you mean by package, what do you mean by package manager, what features you want to have, what features you do not want to have, how you’re going to build and distribute the packages.

    The reason why this is important is that a) this will be a useful reference for the implementation, b) you will learn a lot about the subject matter by trying to answer these questions, and c) you’ll learn to write such documents which is a very useful skill.










  • Rust

    #[derive(Default)]
    pub struct Day1Solver {
        input: Vec<i64>,
    }
    
    impl Solver for Day1Solver {
        fn presolve(&mut self, input: &str) {
            self.input = input
                .trim()
                .split("\n")
                .map(|line| {
                    if let Some(n) = line.strip_prefix('L') {
                        -n.parse::<i64>().unwrap()
                    } else if let Some(n) = line.strip_prefix('R') {
                        n.parse().unwrap()
                    } else {
                        panic!("what: {line}");
                    }
                })
                .collect();
        }
    
        fn solve_part_one(&mut self) -> String {
            let mut p = 50;
            let mut count = 0;
            for n in self.input.clone() {
                p += n;
                if p % 100 == 0 {
                    count += 1;
                }
            }
            count.to_string()
        }
    
        fn solve_part_two(&mut self) -> String {
            let mut count = 0;
            let mut p = 1000000000050;
            for i in self.input.clone() {
                if p % 100 == 0 {
                    count += (i / 100).abs();
                } else {
                    count += ((p + i) / 100 - p / 100).abs();
                    if i < 0 && (p + i) % 100 == 0 {
                        count += 1;
                    }
                }
                p += i;
            }
            count.to_string()
        }
    }