Timeline for Binary adder implemented in Rust
Current License: CC BY-SA 4.0
11 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Mar 24, 2023 at 22:19 | history | edited | Davislor | CC BY-SA 4.0 |
added 325 characters in body
|
Mar 24, 2023 at 22:01 | comment | added | Davislor |
@theonlygusti Generally, iterators themselves need to be mut in order to call next on them (because .next() updates the iterator). If you call .iter() , the iterators return non-owning references, which sounds like what you want. If you call .iter_mut() , you get iterators over mutable references, and .into_iter() transfers ownership of the elements. There are also, as I mentioned, iterators that clone instead.
|
|
Mar 24, 2023 at 21:38 | comment | added | user98809 |
I'm still a Rust newbie, so this might not be totally valid, but when I see (mut left_it: in the function signature, the mut keyword makes me think "okay the function wants to modify some of the values inside of the iterator". Doesn't the mut keyword give it permission to do that? Is there a way to take iterable arguments into the function without requiring that permission to mutate be granted?
|
|
Mar 24, 2023 at 20:56 | comment | added | Davislor |
@theonlygusti Now that I understand your reasoning—which is valid; sometimes you want to pad with zero digits, sometime spaces, sometimes null bytes—I don’t think there’s a significantly better way than your original approach. You do, in this case, want to .clone() the user-supplied padding object. (And maybe call it something like that, instead of default , because the two meanings of default confused me.) For a primitive type like char , that’s zero-cost. Just pass it by immutable reference?
|
|
Mar 24, 2023 at 20:49 | comment | added | Davislor |
@theonlygusti Are you thinking of the Cloned trait, for iterators that clone instead of moving or mutating their data? I don’t think Rust makes it possible to stop a type from also implementing some other trait that enables interior mutability. You could also maybe stick it in a const fn , so the compiler will warn you about some things that could have externally-visible side-effects.
|
|
Mar 24, 2023 at 20:16 | comment | added | Davislor |
@theonlygusti That’s a valid use case! Here, I just switched to a bool representation where padding with the default value sufficed.
|
|
Mar 24, 2023 at 14:15 | comment | added | user98809 |
I had a custom default parameter on zip_longest because I don't think the default char is '0' , and I feel like it would be a pain to make a custom type and Default for every zip_longest call? Is there a way to take iterators while also specifying the underlying values won't be mutated?
|
|
Mar 24, 2023 at 10:43 | history | edited | Davislor | CC BY-SA 4.0 |
added 31 characters in body
|
Mar 24, 2023 at 10:21 | history | edited | Davislor | CC BY-SA 4.0 |
added 38 characters in body
|
Mar 24, 2023 at 10:13 | history | edited | Davislor | CC BY-SA 4.0 |
added 45 characters in body
|
Mar 24, 2023 at 9:40 | history | answered | Davislor | CC BY-SA 4.0 |