But under the hood it's all the same https://doc.rust-lang.org/stable/src/core/bool.rs.html#157-1...
(You obviously know this, but it might be a surprise to many people reading this thread.)
Something like toggle is also impossible in languages that favour immutability and forward progress requires creating new variables, like several functional and logic languages.
In effect, if x is a bool, doing x.toggle() is rewritten by the compiler as bool::toggle(&mut x), which can of course modify x.
pub const fn toggle(&mut self)
The "&mut" says that toggle takes a mutable reference to the instance it is called off of. This is also called a mutable borrow. You might thing that would get buried in the program because you don't always check the type signature of every method you call, but there are a few reasons that muts really jump out in rust. The languages immutable by default nature is one and the other is that several IDE have features to specifically draw attention to mutable instances/references. It's a fairly common pattern in rust to have helper methods on a type that take a mutable reference or ownership of self.
TL;DR: Rust's ownership model is very unique. Once it clicks its the cat's pajamas, but experience in other languages don't map well to this part of the language.
> decades of experience from other programming languages
What languages?Primitive types and composite types having the same capabilities is a bit of a value statement for C++ and Rust IMO, and it's missing in a lot of languages downstream (IMO) of the "every variable is a reference to a slot/pointer" philosophy taken by most every managed language
One reason is that C++ doesn't know where to put them, for user defined class, struct or union types these "member functions" as their name suggests live inside the class/ struct/ union definition but for a primitive where would these live?
In Rust they're just impl blocks, for example the method we're discussing here is:
impl bool {
pub const fn toggle(&mut self) {
*self = !*self;
}
}The only piece that wasn't really available and was made in a waterfall design approach was export templates, with the known outcome that no one other than EDG has even bothered implementing it.
Post C++98 is when we got into the "advocacy, persistency, and votes" strategy.
So long as the variable is mutable it's value can be changed
x = x + 1
This is definitely something that Rust does a bit different than most other languages, but it's not anything specific to this method, booleans, or particularly new to the language. Most people will learn about the common different ways of defining a method (via ownership, shared reference, or mutable reference) fairly early on in programming Rust.
An example would be having both `Vector3.Add` and `Vector3.AddToSelf` instance methods as well as third `Vector3::Add` which takes two Vector3s.
https://rust.godbolt.org/z/dPn3Y4r59
The optimizer gives the exact same machine code for both functions, and so it just outputs a single function and gives it an alias.
The compiler has a technique called "inlining" https://en.wikipedia.org/wiki/Inline_expansion
The LLVM backend used by Rust is jokingly said to have a inlining heuristic of "yes" ie it will always try to inline everything because that's usually faster.
I don’t know how to feel about toggle returning neither old nor new value tho.
foo.bar().baz[expensive()].qux.toggle(); long_path_bool ^= true;
but that’s less obvious.https://doc.rust-lang.org/std/collections/hash_map/enum.Entr...
let mut map: HashMap<&str, u32> = HashMap::new();
map.entry("poneyland").or_insert(3);
assert_eq!(map["poneyland"], 3);
It's very ergonomic. foo.bar().baz[expensive()].qux ^= true;Looks like part of the motivation was for things with long names like `some_struct_a.some_field_b.some_subfield_c`, where needing to name the field twice would have pushed people to using `^= true` instead, which is a bit more roundabout to decipher and risks people forgetting the idiom and writing `^= false` by accident. Not necessarily the most useful API, but not unmotivated and harmless to add.
let mut vec = vec![false, false, false, false];
vec[2].toggle();
*vec[2] = !vec[2];
"Good enough" is subjective, but I personally prefer the new way vec.map(|x| !x);
vec.map(bool::toggle);Fast forward to today. After years of using it I find it very good for readability. It became the default in codebases. Nobody complains anymore and absolutely no one is confused.