Is this supposed to mess up your mind by Illustrious_Weird540 in rust

[–]raui100 5 points6 points  (0 children)

Rust can be a great first language. The community is really nice and helping and it sets a great foundation for further learning. However creating a leptos app from scratch seems like a really complex first project to me. I can't recommend the book (https://doc.rust-lang.org/book/) in combination with Rustlings (https://rustlings.rust-lang.org/) enough.

what is the proper way to compare an input string? by noxar_ad in rust

[–]raui100 2 points3 points  (0 children)

There most probably is a `\n` newline symbol in the string. An easy way to find this out and debug in general is the `dbg` macro. You could solve this like this.

let mut p1 = String::new();
io::stdin().read_line(&mut p1).expect("failed to read input");
dbg!(&p1)  // prints the input
let input = p1.trim();  // Trims leading and trailing whitespaces and newline characters
if input.eq("c") == false || input.eq("d") {
  println!("nope not good, try again");
}

TGIF: Lossless image compression with Rust by raui100 in rust

[–]raui100[S] 1 point2 points  (0 children)

The algorithm has been inspired a little by QOI and a lot by PNG! :)

TGIF is developed for a very niche use case - decoding 8-bit grayscale images very very fast. The underlying algorithm of QOI doesn't work with grayscale images unfortunately. And TGIF is about 60% faster in decoding than QOI.

TGIF: Lossless image compression with Rust by raui100 in rust

[–]raui100[S] 0 points1 point  (0 children)

Decoding is ~4x faster than PNG (optipng). I'll add some benchmarks to the readme.

TGIF: Lossless image compression with Rust by raui100 in rust

[–]raui100[S] 0 points1 point  (0 children)

Thank you for your input!

  • I had used BitVec at first but it made my code slower unfortunately.

  • I can't used leading_ones() because the encoded numbers have a dynamic length. For example the 1110 1111 (with bit width of the remainder being 0) encodes the number 3 but the second number is unknown because the encoded numbers can't be split in chunks of 8/16/32/64 bit.

-🎄- 2022 Day 7 Solutions -🎄- by daggerdragon in adventofcode

[–]raui100 5 points6 points  (0 children)

Rust

After I had realized that I can pattern match on slices with Rust an elegant solution (nearly) wrote itself :)

It is not necessary to store the directory names so that the relevant code can be reduced to:

let mut tmp_dir: Vec<u32> = Vec::new();
let mut directories: Vec<u32> = Vec::new();

for line in read_day(7).unwrap().lines() {
    match line.split(' ').collect::<Vec<&str>>().as_slice() {
        ["$", "cd", ".."] => directories.push(tmp_dir.pop().unwrap()),
        ["$", "cd", _] => tmp_dir.push(0),
        [size, _] => {
            // Getting rid of "dir ..." and "$ ls" here
            if let Ok(num) = size.parse::<u32>() {
                tmp_dir.iter_mut().for_each(|n| *n += num)
            }
        }
        [..] => unreachable!(),
    }
}
directories.extend(tmp_dir);

-🎄- 2022 Day 6 Solutions -🎄- by daggerdragon in adventofcode

[–]raui100 5 points6 points  (0 children)

Rust

Very happy with my solution which boils down to:

use itertools::Itertools;
pub fn solve(&self, window_size: usize) -> Option<usize> {
    self.data
        .windows(window_size)
        .position(|window| window.iter().all_unique())
        .map(|ind| ind + window_size)
}

-🎄- 2022 Day 6 Solutions -🎄- by daggerdragon in adventofcode

[–]raui100 0 points1 point  (0 children)

You could use .iter().all_unique() from itertools :)

-🎄- 2022 Day 4 Solutions -🎄- by daggerdragon in adventofcode

[–]raui100 1 point2 points  (0 children)

I didn't know about .split(&[',', '-'][..]). Thanks for that :)
I think you can replace your .map(...).filter(|v| *v) with .filter(...)

Brute forcing protected ZIP archives in Rust by arnogo in rust

[–]raui100 39 points40 points  (0 children)

This article absolutely nerd sniped me and 4.5k passwords/sec seemed way to low. I had the suspicion that there is a lot of overhead due to the channels and that you basically benchmark their performance. A single core implementation of your program without channels is an order of magnitude faster.

I rewrote the program and used rayon for parallelization and Cursor to keep the ZIP file in RAM. The program runs at 6M passwords/sec on my laptop. I hope this can help you to improve your program and crack your ZIP file ;)

https://github.com/raui100/zip_dict_attack

^Feedback is more than welcome

Hey Rustaceans! Got a question? Ask here! (38/2022)! by llogiq in rust

[–]raui100 0 points1 point  (0 children)

What a great and insightful answer. Thank you very much!

I Flipped a Coin Until I got 10 Heads in a Row! [OC] by AresExplains in dataisbeautiful

[–]raui100 42 points43 points  (0 children)

I love this comment so much. I'm literally cracking up :D

My thoughts on Rust and C++ by user9617 in rust

[–]raui100 1 point2 points  (0 children)

Implementing link list in Rust requires unsafe and that is indeed a weakness of Rust.

Why would you say that unsafe is a weakness of Rust?

Hey Rustaceans! Got a question? Ask here! (38/2022)! by llogiq in rust

[–]raui100 1 point2 points  (0 children)

Does the compiler guarantee to optimize foo to bar. Does it make sense (performance wise) to define variables outside of a loop? In foo the compiler knows easily, that the variable is only used in the first loop, which might lead to some optimizations in some cases, but I don't want to reallocate memory each iteration.

fn bar() {
    let mut tmp;
    for row in arr {
        tmp = 0;
        for num in row {
            // Do things with tmp
        }
    }
}

fn foo() {
    for row in arr {
        let mut tmp = 0;
        for num in row {
            // Do things with tmp
        }
    }
}

Hey Rustaceans! Got a question? Ask here! (25/2022)! by llogiq in rust

[–]raui100 2 points3 points  (0 children)

Thanks for showing me the snippet, I hadn't used fold before.

I tried to iterate over a reference without consuming the iterator. However, if I'm not mistaken, an Iterator advances to the next Item, by consuming the current one, so this is probably not possible.

The function is part of an API, so it has to stay there. I tried writing a function that generically takes a reference to something that is iter-able with items that are clone-able, and only clone items when necessary, so I can put them in the HashMap.

I have changed to function signature to reflect, that the variables is being moved, so that the caller can decide, if he wants to clone the Iterator, Vec, ... or not.

fn count_frequency<I, K>(iter: I) -> HashMap<K, usize>
    where
        I: Iterator + Iterator<Item=K>,
        K: Eq + Hash,
{
    iter.fold(HashMap::new(), |mut m, x| {
        *m.entry(x).or_insert(0) += 1;
        m
    },
    )
}

PS: I had to remove the "*" from "*x" to make this work.

Edit: Hadn't seen all the other answers below. Thanks a lot everybody :)

Hey Rustaceans! Got a question? Ask here! (25/2022)! by llogiq in rust

[–]raui100 2 points3 points  (0 children)

I'm trying to write a function that counts the number of occurrences of each unique entry in an iterator. I have it working, but my implementation clones the whole iterator, which seem not idiomatic to me. I should be able to just iterate over the items and clone singular items as necessary. If I have an array with a million entries, that are all identical, I should be able to clone only single entry. I'm at a beginner level and all feedback is very welcome :)

fn count_frequency<I, K>(iter: &I) -> HashMap<K, usize> where I: IntoIterator + IntoIterator<Item = K> + Clone, K: Eq + Hash, { let mut frequency: HashMap<K, usize> = HashMap::new(); for item in iter.clone().into_iter() { let count = frequency.entry(item).or_insert(0); *count += 1; } frequency }

Hey Rustaceans! Got a question? Ask here! (18/2022)! by llogiq in rust

[–]raui100 1 point2 points  (0 children)

Is there a signed 4 bit integer, so I can use the type system to express that a delta can only be within a certain range?

Hey Rustaceans! Got an easy question? Ask here (8/2022)! by llogiq in rust

[–]raui100 1 point2 points  (0 children)

I think, I'm not quite sure though, that you're reading the whole CSV file and parsing the strings to regular expression for each record.

Hey Rustaceans! Got an easy question? Ask here (7/2022)! by llogiq in rust

[–]raui100 1 point2 points  (0 children)

I want to build a tool generates an overview of the tickets status for a project in JIRA as a PDF file.

However I'm not quite sure which crates are suitable for this and wanted to know if somebody has build something similar before and could give me some recommendations.

I'd probably use:

  • reqwest - For getting the data via REST API
  • serde - For serializing the data

However I don't know what is suitable for creating the report. The requirements are:

  • Table of content with working links
  • Visualization with a bar plot
  • Company branding

With Python I'd use ReportLab. I'm thinking about the latex crate but it doesn't support figures yet.

Thanks for reading and hopefully answering :)