Skip to content
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

Proposal - Better control for initialization #18204

Closed
NN--- opened this issue Oct 21, 2014 · 10 comments
Closed

Proposal - Better control for initialization #18204

NN--- opened this issue Oct 21, 2014 · 10 comments

Comments

@NN---
Copy link

NN--- commented Oct 21, 2014

Idea of keywords for better controlling variables initialization.

Samples:

mustinit - Variable that must be initialized before used.

let mut mustinit a = 1; // OK
a = 2; // OK

let mut mustinit x;
f(x); // Error: 'x' is not initialized

let mut mustinit x;
if something { x = 1; } else { x = 2; }
f(x); // OK

let mut mustinit x;
if something { x = 1; }
f(x); // Error: 'x' may not be initialized

initonce - Variable that can be initialized only once and then cannot be changed. It doesn't require the variable to be initialized.

let mut initonce a = 1; // OK
a = 2; // Error 'a' has been already initialized

let mut initonce a;
a = 1; // OK
a = 2; // Error 'a' has been already initialized
@jdm
Copy link
Contributor

jdm commented Oct 21, 2014

How is initonce any different than leaving off the mut qualifier? Similarly, I don't see how the purpose of mustinit - that's exactly how the language behaves today. What's the motivation for making it optional?

@NN---
Copy link
Author

NN--- commented Oct 21, 2014

The purpose is that you can leave variable uninitialized and intiialize it only when you are ready.
But compiler can tell you that you using uninitialized variable.

'initonce' means that you initialize it only once and cannot change its value later.
'let mut' allows you to change the variable more than once.

I am not good in Rust language, perhaps the Rust language already works this way ? :)

@jdm
Copy link
Contributor

jdm commented Oct 21, 2014

let a;
...
a = 5;
//a = 6; <- error, immutable value.

already works. Any attempts to use a before it's initialized will be an error, and that works with constructs like if/else too.

@thestinger
Copy link
Contributor

Yeah, these features already exist.

@NN---
Copy link
Author

NN--- commented Oct 21, 2014

And what about mutable variables ?

@thestinger
Copy link
Contributor

Rust never allows use before initialization.

@thestinger
Copy link
Contributor

If you want to leave a variable uninitialized but can't prove safety to the compiler, then you use an unsafe block and initialize with the uninitialized function.

@NN---
Copy link
Author

NN--- commented Oct 21, 2014

I see.
Great news.
Thanks.

@NN---
Copy link
Author

NN--- commented Oct 27, 2014

The only thing impossible to do is to seal the mutable variable from other changes.
You put the value and you want to prohibit changing it.
let mut a;
a = 1; // ok
a =2; // I want to prohibit this and further ones.

@jdm
Copy link
Contributor

jdm commented Nov 3, 2014

That's identical to an immutable variable. They don't have to be initialized at declaration; they just need to be initialized before first use.

lnicola pushed a commit to lnicola/rust that referenced this issue Oct 8, 2024
Update cc to 1.1.22

This version of `cc` contains [a fix](https://github.com/rust-lang/cc-rs/releases/tag/cc-v1.1.22) to prevent spurious rebuilds. Hopefully this should help avoid the CI issues rustc has been having.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants