So I'm no Rust expert but when I saw: let _ = Foo; my immeidate thought was "this is going to get cleaned up immediately", which turns out to be the case. If you come from a language where braces define scopes and variables only get cleaned up when a variable goes out of scope, Rust's behavior may be surprising but it's consistent and correct. The object is immediately out of scope. Three thoughts: 1. Up until a cert…
Rust Needs an Official Specification
31–40 of 53 posts
Re: Rust Needs an Official Specification
#32Re: Rust Needs an Official Specification
#33Re: Rust Needs an Official Specification
#34I don't think the C++ standard can be held up like that. Many compilers simply ignore it (or ignored it, they're getting better about these things). It's not because there's an omission, it's because writing a compiler that conforms to a thousand page document of rules is hard. And tedious. Some will fall through, there is no "one true C++" that follows the standard perfectly, most fall short. The ambiguity is still…
"The implementation is the specification" is frankly a sophomoric position. It works fine for small projects and projects of limited complexity, but Rust is neither of these.
Re: Rust Needs an Official Specification
#35So I'm no Rust expert but when I saw: let _ = Foo; my immeidate thought was "this is going to get cleaned up immediately", which turns out to be the case. If you come from a language where braces define scopes and variables only get cleaned up when a variable goes out of scope, Rust's behavior may be surprising but it's consistent and correct. The object is immediately out of scope. Three thoughts: 1. Up until a cert…
But in this it is not. let _tmp = Foo; The confusing bit is renaming the object from _tmp to _ changes the rules. I assume that in rust '_' is not an actual name, but a pattern to be matched, but even that the actual rule applied is not obvious (does it match everything? Only the lifetime of objects matched with a name is extended?)
- A _ prefix tells the compiler not to warn you a variable isn't used.
- A _ variable name is a reserved symbol to say something cannot be used. Trying to do so will cause a compiler error.
So they're scoped differently.
Consider this Go snippet:
func A() { fmt.Println("A") }
func B() { fmt.Println("B") }
func main() {
var B = A;
B()
}
What function gets called? It's A but you can reasonably make a case for B given the capitalization of Go function names and variables. My point is that scoping and resolution rules and many other aspects of a language can be viewed as intuitive (or not) based simply on what you're used to. Even then they can be arbitrary.Re: Rust Needs an Official Specification
#36Earlier quoted context omitted.
There are multiple Rust compilers, for example - https://github.com/Rust-GCC/gccrs - https://github.com/thepowersgang/mrustc
they are both incomplete
This comment was showing that there are other projects using the word rust in their name & description.
The progress of those projects doesn't change the fact that they exist and call themselves rust compilers.
Re: Rust Needs an Official Specification
#37Earlier quoted context omitted.
But in this it is not. let _tmp = Foo; The confusing bit is renaming the object from _tmp to _ changes the rules. I assume that in rust '_' is not an actual name, but a pattern to be matched, but even that the actual rule applied is not obvious (does it match everything? Only the lifetime of objects matched with a name is extended?)
Those are two different things. - A _ prefix tells the compiler not to warn you a variable isn't used. - A _ variable name is a reserved symbol to say something cannot be used. Trying to do so will cause a compiler error. So they're scoped differently. Consider this Go snippet: func A() { fmt.Println("A") } func B() { fmt.Println("B") } func main() { var B = A; B() } What function gets called? It's A but you can reas…
But I don't see how's that relevant, this is not about the scope of bindings, but about the lifetime of objects.
As Steve explained in a sibling comment, _ is in fact not a variable name, and doesn't extend the lifetime of the object. The semantic is perfectly reasonable and I'm sure it has a good reason to be that way, but the difference between two similarly looking syntaxes is surprising and violates the principle of least astonishment.
FWIW, C++ temporary lifetime extensions are also surprising in many cases.
[Note: I don't know if rust is defined in term of temporary lifetime extensions, it is just a way to understand it for a c++ programmer like me]
Re: Rust Needs an Official Specification
#38I don't think the C++ standard can be held up like that. Many compilers simply ignore it (or ignored it, they're getting better about these things). It's not because there's an omission, it's because writing a compiler that conforms to a thousand page document of rules is hard. And tedious. Some will fall through, there is no "one true C++" that follows the standard perfectly, most fall short. The ambiguity is still…
The major C++ compilers are perfectly conformant with the standard. Where they can lag is after standards come out. Modules have been a challenge, but other language features get implemented within a few years. At the time of writing, all the major compilers conform with C++ 17 and aside from modules, conform with C++ 20. There absolutely is "one true C++" as set by the standard, and compiler writers have a roadmap f…
Re: Rust Needs an Official Specification
#39Earlier quoted context omitted.
Those are two different things. - A _ prefix tells the compiler not to warn you a variable isn't used. - A _ variable name is a reserved symbol to say something cannot be used. Trying to do so will cause a compiler error. So they're scoped differently. Consider this Go snippet: func A() { fmt.Println("A") } func B() { fmt.Println("B") } func main() { var B = A; B() } What function gets called? It's A but you can reas…
In a language where functions are in the same namespace as variables (like the majority of languages), I would expect A, in a lisp-2 I would expect B. But I don't see how's that relevant, this is not about the scope of bindings, but about the lifetime of objects. As Steve explained in a sibling comment, _ is in fact not a variable name, and doesn't extend the lifetime of the object. The semantic is perfectly reasonab…
Re: Rust Needs an Official Specification
#40I get the author’s point so this may be a useless nitpick but I am not surprised in Rust that drop is called immediately when a value is assigned to _. That’s almost like calling delete in c++ and if you changed the C++ code to a class with a print in the destructor, then called “delete” you would get the same behavior. The “just” underscore in rust I believe is special and basically makes that value inaccessible. So…
For a C++ programmer, the related "suprising" equivalent is to not name the variable. We had a regex linter in our code to ensure locks were named because of bugs from this.