Reputation: 1409
This seems to be a simple question but I am very new to Rust so I am still asking. So, I could not get a way to declare char
in default constructed or leave uninitialized and this is what I did:
let mut r: char = 0 as char;
// let mut r: char; // error
I do not know it is right the way to do it or is there are proper way to do it?
Upvotes: 1
Views: 1025
Reputation: 60493
Simply declaring the variable will leave it uninitialized, but the compiler won't let you use the variable until it has been initialized so its only useful in certain contexts.
There is also no shorthand for default constructing a value. There is the Default
trait that char
implements (defaults to '\0'
) so you can choose to initialize it like so:
let r = char::default();
But depending on what the surrounding code is doing, it may be more succinct to simply set the value explicitly:
let r = '\0'; // or 'a' or '0' or whatever you like
Upvotes: 5