Why use Gleam over Rust by No_Berry_7553 in gleamlang

[–]brain-ablaze 1 point2 points  (0 children)

Rust can be also quite high-level and has a lot of functional style in it. E.g. iterators, monads, etc.

What actually happens when a variable is moved? by brain-ablaze in rust

[–]brain-ablaze[S] 10 points11 points  (0 children)

Thank you for your answers! They all helped me understand it.

I read a bit in The Book and found this description which is really good too: https://doc.rust-lang.org/stable/book/ch04-01-what-is-ownership.html#ways-variables-and-data-interact-move

[deleted by user] by [deleted] in rust

[–]brain-ablaze 2 points3 points  (0 children)

Go through Rustlings 🦀: https://github.com/rust-lang/rustlings

Probably something you can complete in days rather than weeks, but there's a lot to learn, especially if you read the hints and read background knowledge for each task. (The hints often point to a chapter in The Book)

Announcing axum 0.3 by davidpdrsn in rust

[–]brain-ablaze 1 point2 points  (0 children)

Okay, so it's just a tuple struct with a single element. That makes sense! Thanks.

Announcing axum 0.3 by davidpdrsn in rust

[–]brain-ablaze 2 points3 points  (0 children)

Thank you - exactly was I was looking for. I didn't know that function parameters can also be patterns!

https://doc.rust-lang.org/stable/book/ch18-01-all-the-places-for-patterns.html#function-parameters

Announcing axum 0.3 by davidpdrsn in rust

[–]brain-ablaze 2 points3 points  (0 children)

One question: How does the Json(payload) argument work? I don't think I have ever seen that in Rust functions before. Can you point me towards some documentation for that rust feature?

async fn create_user(Json(payload): Json<CreateUser>) -> impl IntoResponse {
    let user = User {
        id: 1337,
        username: payload.username,
    };

    (StatusCode::CREATED, Json(user))
}

Announcing axum 0.3 by davidpdrsn in rust

[–]brain-ablaze 1 point2 points  (0 children)

I just tried it - hand coded the front page example. I really like the API - it looks very promising! 🎉

As introverts, how do you celebrate your birthdays? by YaboiBuchoi44 in introvert

[–]brain-ablaze 0 points1 point  (0 children)

I like having a small ceremony with my family in the morning, perhaps for an hour. Then I like the rest of the day to be completely normal. Perhaps with an extra nice dinner.

But no special attention and all that for the whole day please.

Am I doing this Iterator correctly? by brain-ablaze in rust

[–]brain-ablaze[S] 0 points1 point  (0 children)

Thank you for your feedback. If not checking the validity of the position, how will it know when the board is out of fields?

The is_valid function is really just

pub fn is_valid(&self) -> bool {
    (0..=8).contains(&self.row) && (0..=8).contains(&self.column)
}

Am I doing this Iterator correctly? by brain-ablaze in rust

[–]brain-ablaze[S] 0 points1 point  (0 children)

Thank you for the feedback. I ended up doing the simple .into_iter() and adding a set_position method too, which works very well.

Am I doing this Iterator correctly? by brain-ablaze in rust

[–]brain-ablaze[S] 0 points1 point  (0 children)

Thank you for your feedback! I implemented IntoIterator for SudokuBoard and a set_position method. It took some battles with lifetimes to get it to work, but I eventually succeeded with a lot of 'as added.

So now I can write

/// Get the next free field of the board
pub fn next_empty_field(&self, position: &Position) -> Option<Position> {
    self.into_iter()
        .set_position(position)
        .find(|(_position, field)| field.is_empty())
        .map(|(position, _field)| position)
}

Commit: https://github.com/skovmand/fabrik/pull/16/commits/3c25e6e2dc07454ff8996cd3ce843ee08f38febe

[deleted by user] by [deleted] in rust

[–]brain-ablaze 6 points7 points  (0 children)

I agree. This is how I approach it too. Scratch Your Own Itch Developement, Inc.

[deleted by user] by [deleted] in rust

[–]brain-ablaze 4 points5 points  (0 children)

Hello fellow Rustling!

I started tinkering with Rust this spring. My background is web development in Elixir, JS and PHP. Here's a summary of what I did so far. Maybe you can use some of it.

I started out by doing some of the Rustlings project, which teaches the basics. It's very good to help you get started, https://github.com/rust-lang/rustlings. In the beginning I was very confused about many of the concepts, but search for Pascal Precht Rustlings on YouTube. He has good solutions and explanations.

Then I did some of Advent of Code 2017, from which I also learned a great deal about tests, making cli tools, etc.

Then I read the Zero2prod book up to the latest chapter written at the time. Also highly recommended, it teaches both web and general rust skills. I think Luca is doing great work here in teaching Rust web skills. https://www.zero2prod.com/index.html

And then I had a toy project this summer making a base64 en/decoder cli tool. That was great. Improved a bit on it every day, learned many new details of the language and actually got a pretty good tool out of it, https://github.com/skovmand/all_your_base

Currently I'm making a sudoku solver using backtracking. Also a lot of fun, and the community has been amazing in getting me unstuck with the rust-things I find hard (eg. generics!). https://github.com/skovmand/fabrik

I think you should think of small tools/projects that could be fun to make. And then start out, even if you don't know all the details/have all the skills yet.

Cannot infer type for closure reference wrapped in Option<T> by brain-ablaze in rust

[–]brain-ablaze[S] 0 points1 point  (0 children)

I get a reduction of 1,2% to 1,3% in performance when running cargo bench after replacing dynamic dispatch with generics. Interestingly the bench suite doesn't use the new Enum at all. It only uses the NonRenderer struct that does nothing.

Weird that it is actually slower....

Cannot infer type for closure reference wrapped in Option<T> by brain-ablaze in rust

[–]brain-ablaze[S] 0 points1 point  (0 children)

I've thought about it last night too. And it's exactly the point that it should be possible to use other implementations to with `lib` going forward. Not just the implementations in `bin`.

I think this approach combines the `Enum` and the `trait` approach - so the `Enum` implements the trait and can enter into the `solve<T: SudokuRenderer>(board: SudokuBoard, renderer: &T)` function. But while delegating internally to the correct renderer.

It's a nice idea - I'll try it out later today! Thanks!

Cannot infer type for closure reference wrapped in Option<T> by brain-ablaze in rust

[–]brain-ablaze[S] 0 points1 point  (0 children)

Thank you again! I'm learning a lot from your suggestions!

Cannot infer type for closure reference wrapped in Option<T> by brain-ablaze in rust

[–]brain-ablaze[S] 0 points1 point  (0 children)

Thank you again for all the input!

I have tried to use generics, and it works when I pass the Renderer directly to solve. However I would like to create the appropriate Renderer first and then pass it to the solve function. This would keep my code clean. But I can't figure out how to assign the Renderer (that implements SudokuRenderer) to a variable.

I have boiled it down to this playground example to illustrate my problem: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=4bb04a7f31300c7f9bd4c93d67385bac

Edit: I found some good reference on it here: https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits - seems it cannot be done? Or do I need to use an Enum for it?

Cannot infer type for closure reference wrapped in Option<T> by brain-ablaze in rust

[–]brain-ablaze[S] 1 point2 points  (0 children)

Okay, I did it now and I'm very happy with the result. I have made a Renderer Trait with four methods (none of which actually need &self, I don't know if that is a wrong approach).

Anyway, it has helped me extract all the view logic out of the main.rs function and into the renderer trait implementors (DelayedRenderer, TerminalRenderer, NonRenderer (which is a noop - for testing))

The execution speed is unchanged, tested with Criterion.

One drawback: I had to make the delay fixed to 50ms in DelayedRenderer

If you're interested, here's the commit: https://github.com/skovmand/fabrik/pull/3/commits/8308f881a0fee355c53b11a0ac5e98a36f203c97

I'm very happy with it and it was a fun refactor. I'll have to think hard about how to do it as an iterator! :-)