Feature Request: Toggle Variable type

This might be a too simple an idea or already suggested, but in case it isn't:

I would like to suggest a new variable type called toggle. It would be effectively a boolean true/false that automatically flips every time the memory address is accessed (unless a peek funtion is used).

I would also suggest that instead of potentially being undefined or false on an empty initialization toggle variable would be initially true.

This new variable type I suspect could save a few lines of code here and there.

I would also suggest that instead of potentially being undefined or false on an empty initialization toggle variable would be initially true.

How would a variable "know" if it was initialized yet?

Without the initialization magic you can easily implement such a type yourself in a library. If it gets a significant user count you can argue that such a thing belongs in the standard library.

lines of code here and there

Can you give some examples?

What I mean by initialization would be some like this (psudo c code):
bool isOddLoop1;
toggle isOddLoop2;
while(true){
print( isOddLoop1 ); //prints undefined or false always
print( isOddLoop2 ); //prints TRUE then FALSE the TRUE....
}

As for making a library yes it could be a library like anything else but adaptation and use would be low. I image if I personally made a library it would be unsecure, unmaintained and very poorly optimized.

What's the problem with this?

pub struct Toggle(bool);
impl Toggle {
  pub fn new(value: bool) -> Self {
    Self(value)
  }

  // toggle and returns old value
  pub fn toggle(&mut self) -> bool {
    self.0 = !self.0;
    !self.0
  }
}
2 Likes

Nothing, (as far as I can tell). It's just that everyone importing a library or inventing that code individually causes it to be non-standard (and probably not optimized). Your code is a perfect example of the higher level of programming understanding to implement a toggle vs the relative low level needed to understand and use a toggle.

Imagine how difficult it would be for new programmers if they had to to individually invent Strings from scratch.

A lot more people use strings than they would use this toggle type.

2 Likes

This is impossible in Rust.

let isOddLoop1: bool;
print( isOddLoop1 ); 

you get compile time error for usage of uninitialized variable.

Toggle is an incredibly trivial type. If you can't implement it, you should probably learn how.

Also, what are the use cases for this? I don't know that I ever had the need of it. A simple bool that I turn on or off after something happens, sure. But that tends to latch to the new value. And complex multi-state state machines that change back and forth between many states, sure.

A 2-state FSM like this? You are going to need to show solid use cases for why this would need to be in the standard library. Perhaps go look at some previous library RFCs to see what level of evidence for the need is expected in Rust.

3 Likes

What's the problem with a non-standard Toggle type? In other words, can you provide an actual example were a missing standard for Toogle is harming interoperability?

What's there to optimize in Toggle?

Likewise imagine how difficult it would be for new programmers if the standard library included everything you could possibly need (because why stop a Toggle type then) and they had to learn all of it.

Can you give some examples? I'm having a hard time picturing where I'd use this.

Your description sounds kind of like you want a heartbeat, which can already be done with iterators. Here is an example:

let mut iter = std::iter::repeat([true, false]).flatten();
dbg!(iter.next().unwrap());
dbg!(iter.next().unwrap());
dbg!(iter.next().unwrap());
4 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.