-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New useful macro to std , mut! #2716
Comments
How about |
I don't think that this ahould be added because Rust wants you to avoid mutation where possible, and this would be counter to that goal. Similarly with let mut (a, b, c) = (5, 3, "hello"); |
@KrishnaSannasi It still makes you think "Do I really need this to be mutable?" , while making the code more readable. |
Yes, but let's say you already have a |
This was decided against in #2401. |
@KrishnaSannasi It's just that the "mut!" macro is a little bit harder to write and to look at (it's a whole block). That's the syntax salt here. if needed you can even go as far as to purposely make the name of the macro longer. It's not about making the user write bad code to get a mutable variable it's about making it a little bit less convenient and letting the user know he is using the language in a way it wasn't designed to be used and the whole macro thing gets the point across. |
let (mut x, mut y) = ...; to let (mut x, mut y, z) = ...; Than to let (mut x, mut y, mut z) = ...; So Rust still favors immutability.
Rust doesn't want to make it easier to make mutable variables, so making it more convenient isn't that great. Also, adding
Disclaimer This section is mostly my personal taste and opinion, and reflects my habits when programming. No, it would be about the same to add it to either, it may even be easier to add it to the mut! {
var_name = val,
foo = val,
bar = val,
} Now you just need to go to a line in the macro, press enter and viola you can make a new variable. Compare that to adding it to a let binding, you have to go to the end of the tuple pattern (which can be a little tedious as getting right before the paren is annoying), then you can add a new binding. Or you can do The cost of the Finally, a |
The keyword |
@Centril I don't think that is a big issue, we could follow suit with the library and name it |
@Centril it's not about how you call it it's about the idea itself. I acknowledge that calling it "mut" was a bad idea impossible to implement. Something like "mutables" should do. "var" can be confusing with other languages (we don't want people to write Rust like it's JavaScript). But practically how you call it doesn't matter it's a useful macro to have. |
@TomerZeitune This is basically the same as |
As a meta note, Rust really prefers to keep things in third-party crates as much as possible. Rust tries hard to not move things into the stdlib unless there's a very compelling reason to do so. Third-party crates are great: they can be independently versioned, they can have breaking changes, they can do crazy experiments that cannot be done in the stdlib, etc. If something goes into the stdlib, it's there forever, and it can never change. That has caused a lot of problems with other languages (e.g. Python), which is why Rust is so against it. |
@KrishnaSannasi It's not that it's fundamentally wrong to use it it's just that it shouldn't be the default for a variable. I don't think this macro can or will make mutable variables the go to default in Rust for various reasons I described above. I think the name can also suggest how you should use it. So by having "mut" somewhere in the name we automatically make it very clear that you should use it for when you need mutable variables. Not for every variable like the name "var" suggests . I personally use this lib and it's really a much more subtle change than you might think. |
Recently I came across this amazing crate on crates.io.
Simple useful and reads well.
I suggest making it part of the standard library.
But to avoid confusion with other languages change the macro to
mut!
.Example:
Is much more readable than
let (mut a, mut b, mut c) = (5, 3, ”hello”);
The text was updated successfully, but these errors were encountered: