Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Ihmemies
Oct 6, 2012

So has the time of Rust enjoyers finally come? :bederper:

Consumer reports advocates memory-safe languages like Rust, instead of legacy languages like C/C++:
https://advocacy.consumerreports.org/research/report-future-of-memory-safety/
So does NSA: https://www.nsa.gov/Press-Room/News...-safety-issues/
Noticeable parts of Windows have already been rewritten in Rust: https://www.theregister.com/2023/04/27/microsoft_windows_rust/
Apparently Rust was accepted as an language for linux kernel development etc.

Our School still teaches C++ and ANSI C (ugh :shepicide:). No rust courses. I didn't get any job for summer, so I guess it's time to learn rust? :v:
Someone said this is more comperehensive: https://doc.rust-lang.org/stable/book/ than this: https://doc.rust-lang.org/rust-by-example/

Any pro tips on which to use? I guess Vscode is an ok editor for Rust, if I configure it as you suggested earlier in the thread.

Adbot
ADBOT LOVES YOU

Ihmemies
Oct 6, 2012

Fffuuu.

I need to implement a

Rust code:
fn josephus(mut n:usize, mut k:usize) -> String {}
Where 1 <= n <= 2*10^5.

I am in deep crap. Do I have some other options than a circular linked list, implemented with raw pointers? I don't know how to do it but I can learn, can't be much tougher than in ANSI C.

Ihmemies
Oct 6, 2012

Dijkstracula posted:

I don’t really remember precisely what the Josephus problem is, but what limits you from using a VecDeque?

There’s also a linked list implementation in the standard library too.

Consider a game where there are n children (numbered 1,2,..,n) in a circle. During the game, every kth child is removed from the circle until there are no children left. In which order will the children be removed?

And I need to return a list of removed persons, in the removal order. My futile attempt was too slow:

Rust code:
fn josephus_slow(mut n:usize, mut k:usize) -> String {
    let mut people: Vec<usize> = (1..n + 1).collect();
    let mut idx = 0;
    let mut sequence: Vec<usize> = vec![];

    while !people.is_empty() {
        idx = (idx + k - 1) % people.len();
        // Probably WAY TOO SLOW.. remove runs O(n)
        sequence.push(people.remove(idx));
    }
    
    sequence
        .iter()
        .map(|&n| n.to_string())
        .collect::<Vec<String>>()
        .join(" ")
}
How would a vecdeque help? Rust's Linked list is Doubly-linked and really supports removing only elements from either end.

Ihmemies
Oct 6, 2012

VecDeque helped somewhat, but not still enough. Maybe smaller integers? I finally actually timed what takes so long, and I realized printing!

Rust code:
fn josephus(mut n:i32, mut k:i32) {
    let mut people: VecDeque<i32> = (1..n + 1).collect();
    let mut idx = 0;

    let stdout = io::stdout();
    let lock = stdout.lock();
    let mut w = io::BufWriter::new(lock);

    while !people.is_empty() {
        idx = (idx + k - 1) % people.len() as i32;
        write!(w, "{} ", people.remove(idx as usize).unwrap()).unwrap();
    }
    w.flush().unwrap();
}
Writing data straight out like that is way faster than using either print or println or storing the values anywhere.

Pointer usage will be saved for another day. Thanks...

Ihmemies
Oct 6, 2012

gonadic io posted:

is locking stdout for a long time like that an issue? I don't really know what its implications are tbh. I personally would be tempted to populate a vec then write out in one go after

I tried that first, populate a vector with ints. Use println! and iterators to print it in one go. I could not get either print or println work fast enough. With n=200000 writing to stdout gave a 0,4s execution time. With any kind of print it was over 1s with my (lack of) skill.

Ihmemies
Oct 6, 2012

This is just an algorithm course, so nothing serious. I began learning rust two weeks ago, because I needed a compiler which babysits me.

We have 100 different algorithms to implement, and now after a soft start the tasks seem to get stricter. One thing I did not try out was storing the values to vector and then writing it instead of printing. Iterating a vector of 200k indexes adds an extra delay though, so just writing the data after each calculation is done seems to be faster.

Ihmemies
Oct 6, 2012

Before implementing a command line argument tool in "main" bin, I used a bin for each day in advent of code.

Now I wonder how can I run only tests for day_13.rs for example. They are in end of day_13.rs

Rust code:

#[cfg(test)]
mod tests {
    use crate::utils::{self, read_data_from_file};
    use super::*;   

    #[test]
    fn test_day13() {
        test_silver();
        test_gold();
    }  

    #[test]
    fn test_silver() {
        let test_data:Vec<String> = read_data_from_file("input/real/13.txt");
        assert_eq!(silver(&test_data), 27502);
    }

    #[test]
    fn test_gold() {
        let test_data:Vec<String> = read_data_from_file("input/real/13.txt");
        assert_eq!(gold(&test_data), 31947);
    }
}
cargo test test_day13 runs all the very non-existing tests from all of my binaries. Is there some way to configure it to run actually only the tests in day_13.rs ..?

It just spams my terminal with useless crap currently :I

E: Heh, the "Run Test" button above mod tests in day_13.rs in VSCode magically told me the correct command:

code:
cargo test --bin main -- day_13::tests
Now I just need to remember that!

Ihmemies fucked around with this message at 19:35 on Dec 13, 2023

Ihmemies
Oct 6, 2012

bitprophet posted:

There’s also a half decent cargo-aoc plugin out there which greatly simplifies this stuff, if wrestling with a cli tool isn’t something you actively enjoy doing. (Its “generator” feature is flaky but also just not necessary.)

I run the code on my linux server, because setting up stuff there is so much easier. So program runs on VSCode's terminal window.

You mean this onehttps://crates.io/crates/cargo-aoc ? Hmm maybe I should test it, thanks. Although since I started learning rust like 2 weeks ago maybe it's good first to do things more manually.

Adbot
ADBOT LOVES YOU

Ihmemies
Oct 6, 2012

I am learning structs are quite handy. For a simple struct like this:

Rust code:
#[derive(Clone, Eq, Hash, PartialEq)]
pub struct Coord {
    pub x: isize,
    pub y: isize,
}
You can implement features like this:



So it checks if the coord fits certain bounds. AoC has different sizes of maps and doing always the same bounds checking individually in different solutions starts to suck.

Then I realized you can write cool comments like that, so my IDE shows tooltips for my own functions, just like it does for STD's functions. Sweet..

AoC day 17 progress is not great, but not terrible. My utils.rs is growing in functionality at least.

First I used usize as coords, then I thought about what if I want to take a cruise around origin.. and realized I need to support negative coordinates too.

Ihmemies fucked around with this message at 17:29 on Dec 17, 2023

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply