| ▲ | TinkersW 11 hours ago | |
In C++ you can do: int i = is_tuesday ? 0 : 1; | ||
| ▲ | ninkendo 11 hours ago | parent [-] | |
Well, sure, in Rust you can do `let i = if is_tuesday { 0 } else { 1 };`, what's your point? Imagine it's 10 variables instead of one, and the value you set them to is based on much more complex/nested logic. Sometimes for readability it's nice to declare and specify their types up front, then you go about initializing them all via some complex logic, then you use them later. The compiler should guarantee that they're all initialized before use. There's no real way to do this in C++ every time: For primitives it works, but for other types, it needs a no-argument constructor available or the code won't compile, and even if a no-argument constructor is available, the compiler will needlessly run that constructor even though it doesn't need to (and maybe it'll optimize that away but who knows.) You can obviously make this work in C++ by (subjectively) decreasing readability, but wouldn't it be nicer if declaration was an explicit thing separate from initialization, and the compiler would simply enforce that it's safe? | ||