Member-only story

The consistency of Rust

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

Donald Le
1 min readSep 17, 2020
Photo by Kristaps Ungurs on Unsplash

Consider this example:

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/main.rs:6:9
|
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~~~

--

--

Donald Le
Donald Le

Written by Donald Le

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

No responses yet