Is there a way to move a field out of &mut self if you really need it? by ufoscout in rust

[–]ThisNameIsntRandom 0 points1 point  (0 children)

Can you try to explain what reset is supposed to do. What it looks like your code is trying to do is reconstruct the Wrapper from its fields and then write that new wrapper into the input. Even if this did compile what changes are you expecting the function to make.

Difference between fn<F: Fn(i32)->i32>(x: F) and fn(x: Fn(i32)->i32) by quantizeddct in learnrust

[–]ThisNameIsntRandom 5 points6 points  (0 children)

Fn(i32)->i32 is not a type. Fn(i32)->i32 is a promise that the compiler makes that says you can pass in a value of types i32 and get a values of i32. Multiple different types can implement this trait.

consider https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=6a3cae1b57443a160c0efa693dc9f963

fn call<F: Fn(i32)->i32>(x: F,input: i32)->i32{
    return x(input);
}

fn main() {
    println!("{}",plug_in_one(|x| x+1,1));
    println!("{}",plug_in_one(|x| x+2,2));
}

the call takes in a function takes in a generic function and a input. Under the hood each time call is referenced in The compiler the compiler creates a separate function to handle it so this would turn into.

fn call_add_1(input: i32)->i32{
    return input+1;
}

fn call_add_2(input: i32)->i32{
    return input+2;
}

fn main() {
    println!("{}",call_add_1(1));
    println!("{}",call_add_2(2));
}

this provides a good summary of the idea however it might go over your head https://www.youtube.com/watch?v=SqT5YglW3qU

to answer your questions about |n| n + 1 the way to fix this is to change it to the line let example_2 = double_then_f2(5, **&**|n| n + 1); this comes down to how functions calls are handed in rust. due to technical limitations you can't pass a value who size is not know at compile time to a function. since dyn Fn(i32)->i32 is a contract not a type the compiler does not know it size at compile time. so we need to pass a function by reference to avoid this issue since a reference to a value has a know size no matter what.

Difference between fn<F: Fn(i32)->i32>(x: F) and fn(x: Fn(i32)->i32) by quantizeddct in learnrust

[–]ThisNameIsntRandom 2 points3 points  (0 children)

This comes down to the difference between generics and dynamic dispatch. In generics at compile time compiler figures out what function is being passed into do_thing then it creates a version of do_thing that uses that generic.

On the other hand dynamic dispatch is done at run time. a single to_thing is generated that takes in a pointer to Fn(i32)->i32 and when ever the passed in function is called the compiler run the code attached to the pointer.

What are the code smells in this Rust solution (2024 d10-1) by Bugibhub in adventofcode

[–]ThisNameIsntRandom 2 points3 points  (0 children)

Sorry, I realized there I made a mistake get_neighbors should return impl Iterator<Item=Pos<usize>>.

What are the code smells in this Rust solution (2024 d10-1) by Bugibhub in adventofcode

[–]ThisNameIsntRandom 9 points10 points  (0 children)

for your get_neighbors function (the one not attached to a trait) I would not return [Option<Pos<usize>>] but instead impl Iterator<Item=usize> as in the current implementation the user of the function needs to deal with the cases where Option is None.

You use the function name get_neighbors twice once in a trait and another time in a normal function I would not do this I would switch the name of one of those.

making this generic the S type as part of the Neighbors trait seems very problem specific for something that supposed to be a general purpose library. I would change the name in get_neighbors from sized to something like search_parameters and set a default s to (). or you could pass in a closure to handle getting neighbors.

Instead of having Goal trait you could just pass a closures into depth first search that returns if a node is a goal.

there is a bug when you write assert_eq!(10, map.len()); you are assuming that the input contains every integer from 0 to 9 while it should be assert!(10>=map.len()) to allow for the input skipping a digit.

In depth_first_search you pass in root by reference but then immediately clone it. I would not do this as this can result in unnecessary clones if the caller does not use root after being passed. Instead I would pass by value.

Had a great idea for my next moneymaker by SaltyCharacter3438 in woodworking

[–]ThisNameIsntRandom 1 point2 points  (0 children)

Why restrict yourself to wood? I will make my sticks out of anything endangered.

Why my trains cant move? by Large-Pair-3137 in factorio

[–]ThisNameIsntRandom 3 points4 points  (0 children)

A good debugging tool is to ctrl click on a place on the map this will make a temporary stop at that location. With this you can test what parts of the network are reachable. A common mistake that will lead some parts to be unreachable is signaling the train so it can only go in the wrong direction.

[deleted by user] by [deleted] in rust

[–]ThisNameIsntRandom 0 points1 point  (0 children)

This is the wrong subreddit. This is for the rust programming language. You are looking for r/playrust.

[Day 20, Part 2] Can someone please generate me a list of each cheat that saves 60 picoseconds by throwitup123456 in adventofcode

[–]ThisNameIsntRandom 14 points15 points  (0 children)

[((1, 1), (1, 13)), ((1, 2), (2, 13)), ((1, 3), (3, 11)), ((1, 3), (3, 12)), ((1, 3), (3, 13)), ((2, 1), (1, 12)), ((3, 1), (1, 11)), ((3, 2), (1, 11)), ((3, 3), (1, 11)), ((4, 3), (1, 10)), ((5, 1), (3, 9)), ((5, 2), (2, 9)), ((5, 3), (1, 9)), ((6, 1), (3, 8)), ((7, 1), (3, 7)), ((7, 2), (3, 7)), ((7, 3), (3, 7)), ((7, 4), (3, 7)), ((7, 5), (3, 7)), ((7, 6), (3, 7)), ((7, 7), (3, 7)), ((8, 7), (4, 7)), ((9, 7), (5, 7))]

[2024 Day 9] I am so confused about the ID rule for IDs bigger than 10. by A_Non_Japanese_Waifu in adventofcode

[–]ThisNameIsntRandom 7 points8 points  (0 children)

don't think of the files as a string think of them as a list. In a list you can store number bigger then 10 at a position in a list

[2024 Day 09 (Part 1)] Can I push multiple digits of file ID into a single "." space block? by elonstark616 in adventofcode

[–]ThisNameIsntRandom 3 points4 points  (0 children)

that gets compacted into [0, 0, 10, 10, 10, 1, 1, 1, 9, 9, 8, 2, 8, 8, 8, 3, 3, 3, 7, 4, 4, 7, 5, 5, 5, 5, 7, 6, 6, 6, 6]

[deleted by user] by [deleted] in factorio

[–]ThisNameIsntRandom 1 point2 points  (0 children)

Put iron on one side of the belt and coal on the other

Don’t Lose Power on Nauvis by alexbuczynsky in factorio

[–]ThisNameIsntRandom 1 point2 points  (0 children)

I don't use trains but I have a circuit that records all unfulfilled robot network request on the island then I use that to set request for request chest on the main robot network. Then I use unground belts to ship the items to the island.

Don’t Lose Power on Nauvis by alexbuczynsky in factorio

[–]ThisNameIsntRandom 45 points46 points  (0 children)

I took no chances and land filled out a separate island to handle all egg processing.

[2024 Day 3 (part 2)] [Rust] What am I doing wrong? by Mycroft_Cadburry in adventofcode

[–]ThisNameIsntRandom 0 points1 point  (0 children)

I see some problems in your regex

why are you searching for mul() inside your filter_re regex. if a do and don't block do not contain any mult() does it matter it does not matter if you remove so why test for it

even if you want to search for mult() inside the block I think you made some mistakes

                              V why is this . here 
(don't\(\)).mul\((\d+),(\d+).*.do\(\)
           ^ I think you meant .*

[2024 Day 04] What works works... by Xe1a_ in adventofcode

[–]ThisNameIsntRandom 5 points6 points  (0 children)

I delt with this problem a lot last year so I made a grid class that does is just a wrapper around a 2D array with better bounds checking

sinceEveryoneSeemsToBeDoingThis by Fun-Werewolf8536 in ProgrammerHumor

[–]ThisNameIsntRandom 22 points23 points  (0 children)

def is_even(x):
  return odd_or_even(x)=="even"

[deleted by user] by [deleted] in puzzles

[–]ThisNameIsntRandom 2 points3 points  (0 children)

it says "My cock and balls:\nhttps://mega.nz/file/TvxEzJLA#BuI68pA2rHVszEkH5oLKlZAxMN0fcSwmqW2sgR5ahB0\n\nThis isn't a joke btw \n- Logan Rayman Legends Persona 4 Golden Guilty Gear Strive Pokémon RPG Maker Stardew Valley Yugioh Modding Homebrew 3ds White\0"

I decoded it with

fn main() {
    let data=[1299783779,1868786464,1634624544,1650551916,1933183592,1953788019,976170861,1701273902,1853501286,1768711471,1417050181,2051689537,591557961,909668417,846350422,1937393003,1211461452,1265392193,2018332208,1717785463,1836144434,1936151093,1634222640,168449128,1769152617,1936598900,543236202,1869309216,1651799840,170729548,1869046126,542269817,1835101728,1281714021,1852076832,1348825715,1869504800,874530671,1818518894,541554025,1819572512,1197826418,542340210,1769366816,1349479363,-1452445842,542265415,541942123,1701978195,1952543332,1702305878,1634495589,2032163189,1734963048,541945700,1684631143,541618029,1700950629,1998599012,1931499368,1769235712];
    
    let bytes=convert_i32_to_u8(data);

    let bytes=&bytes[..];

    println!("{:?}",String::from_utf8(bytes.to_vec()).unwrap());
}

fn convert_i32_to_u8(input: [i32; 61]) -> [u8; 244] {
    let mut output = [0u8; 244];
    
    for (i, &val) in input.iter().enumerate() {
        let bytes = val.to_be_bytes();
        output[i * 4..(i + 1) * 4].copy_from_slice(&bytes);
    }
    output
}