The consistency of Rust

Rust is consistent, and it helps to prevent future bugs.

Photo by Kristaps Ungurs on Unsplash

Consider this example:

consistent rust

Running this will show incompatible error type which is interesting. Guess why?

Because the value of if condition is int type but in else is string type.

error[E0308]: `if` and `else` have incompatible types
--> src/
3 | let number = if condition {
| __________________-
4 | | 5
| | - expected because of this
5 | | } else {
6 | | "six"
| | ^^^^^ expected integer, found `&str`
7 | | };
| |_____- `if` and `else` have incompatible types

Change to this :

Will succeed.

Compiling rust-getstarted v0.1.0 (/home/cuongld/GolandProjects/rust-getstarted)
Finished dev [unoptimized + debuginfo] target(s) in 0.24s
Running `target/debug/rust-getstarted`
The value of number is: 8

~~Happy coding~~~

A passionate automation engineer who strongly believes in “A man can do anything he wants if he puts in the work”.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store