-
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
Make square bracket []
more usable.
#3583
Comments
I generally like the idea of |
The problem being that |
Yes, i meant operationally first call such |
The let x: Vec<String>;
let y: usize;
let z: String;
x[y]_? = z;
// equivalent to
*(x.try_index_mut(y)?) = z;
// fn try_index_mut(&mut self, key: usize) -> Result<&mut String, SomeError>; The implicit dereference means method chaining is not possible without context-dependent lowering rules x[y]_.inspect_err(|err| warn!(err, "{y} did not exist"))? = z; |
Interesting idea, but underscore looks ugly and inconvenient to type after
This could also be helpful be with maps:
|
chained fn lol(huh: Option<Option<Option<Option<Option<()>>>>>) -> Option<()> {
huh????
} |
you could do !? like in haskell, its a good compromise between the uglyness of _? and ?? already being taken. let arr = [1,2,3];
arr[3]!?; // returns Option<{Integer}> |
|
The older I get, the less important I feel syntax-centric discussions are. Here is my proposal, take it or leave it: array[?index] As far as I am aware of, that is not taken. However, we might want to CC the Effects WG, since they might have their own ideas about this as this issue overlaps with their work. Should we continue the discussion on Zulip? |
TL;DR version: Make
vec[key]_? = new_value;
doesn't panic when key is out of bounds.For Rust syntax budget, square bracket is perhaps one of the least rewarding current design at all. Every occurrence of square bracket reminds people the potential possibility of panicking. This might be suitable for the "error early if something goes wrong" style application, but for long running services, propagating the panic to the root of the thread callstack might not be a good idea.
This proposes putting an
_
(underscore) between the bracket and the?
operator to invoke newTryIndex{,Mut}
traits instead, returning aResult
with an error type, then perform?
operator on that to return early.The text was updated successfully, but these errors were encountered: