Learning Rust Pt. 6
Writing a condition is quite easy in Rust and follow simple and intuitive syntax and rule.
- No need to write the condition within bracket.
fn main() {
let x = 4;
if x % 2 == 0 {
println!("{} is even.", x);
}
}
- The expression must be evaluated to boolean value, otherwise, Rust will complain.
fn main() {
let x = 4;
if x {
println!("{} is even.", x);
}
}
As usual, we've the else with if.
fn main() {
let x = 4;
if x % 2 == 0 {
println!("{} is even.", x);
} else {
println!("{} is odd.", x);
}
}
Or you can even use else if in some specific scenario.
fn main() {
let x = 4;
if x > 0 {
println!("{} is positive.", x);
} else if x < 0 {
println!("{} is negative.", x);
} else {
println!("{} must be zero.", x);
}
}
Learning Rust Pt. 5
Rust is static typed language and we need to define data with the types. The above program works due to a concept called the type inference which is supported by Rust nicely.
When you assign an initial value to a variable, Rust will infer the initial value and based on it, it'll auto assign the default type good type. In following example, the type of x will be i32 as an integer.
fn main() {
let x: i32 = 12;
println!("{}", x);
}
We've few integer data types in Rust such as i8, i16, i32, i64, and i128 as signed integer and u8, u16, u32, u64 and u128 as unsigned integer.
fn main() {
let x: u8 = 12;
println!("{}", x);
}
If the assigned value contains decimal point in it, it'll be treated as floating-point number and the default value will be f64. For the float, we've f32 and f64 and f64 being the default one.
fn main() {
let x = 13.3;
println!("{}", x);
}
As usual, we've more primitive data types as we have found in other programming language such as char for character with single character. The main difference in Rust that you'll notice is, you can have the UTF-8 character. The default and the only type for the character is char.
fn main() {
let x = 'ðĨģ';
println!("{}", x);
}
Finally, the bool or boolean is yet another primitive data type that has true and false as one of the possible values.
fn main() {
let x: bool = true;
println!("{}", x);
}
We've seen the string in double quotes. But, string is not the primitive data type in Rust. We'll talk about string in future.
Learning Rust Pt. 4
let keyword is used to define a variable in Rust.
fn main() {
let name = "Bruce";
println!("Hello, {}", name);
}
We can define as many variables as we want using let. But, we need to use them once defined. Otherwise, compiler will throw a warning (not error) as unused variables.
fn main() {
let name = "Bruce";
let age = 29;
println!("Hello, {}", name);
}
Even further, compiler also suggests the variable to prefix with _ if variable is defined in this way (defined but no plan to use for now) to remove the warning.
let actually create an immutable variable. In other words, you can't change the value of it once defined. You'll get an error about assigning the value to immutable variable.
fn main() {
let x = 12;
x = 13;
println!("{}", x);
}
If we're intentionally plan to change the value as suggested above, we need to mut the variable.
fn main() {
let mut x = 12;
x = 13;
println!("{}", x);
}
But, this again throw a warning about unused assignment as value 12 that is assigned in above program is re-assigned again before it actually used. So, assigning 12 is useless.
Learning Rust Pt. 3
We can print as many text on the terminal as we want using println!() macro. These text are known as string in Rust and must be written enclosed in double quotes only.
fn main() {
println!("hello, world");
println!("how are you?");
}
{} within println!() macro is used to substitute the values from associated arguments.
fn main() {
println!("hello, {}", "world");
println!("how are {}?", "you");
}
You can have more than one upto as many as you want substitution in println!() macro. The order is important in argument. See the second println!() as an example.
fn main() {
println!("{}, {}", "hello" "world");
println!("{} {} {}?", "how", "you", "are");
}
Learning Rust Pt. 2
Rust files are normally identifies via .rs extension. A journey with programming language should always starts with hello, world program and here is the Rust's one in hello.rs.
fn main() { println!("hello, world"); }
Being a compiled programming language, we need to invoke a Rust compiler or rustc on this program.
rustc hello.rs
Compile will create an executable (if error not found) usually named after the program file without extension that is easily run!
./hello hello, world
And there you have the hello, world!
fnis used to define a function in Rust.main()function is an important one from where the execution starts from.println!()is a macro that will print hello, world in terminal with newline at the end.!afterprintln!()means it is a macro and not a function.- You don't have to worry about the difference between macro and function for now (If you're still insisting, here is one answer from StackOverflow might helpful for you).
- Semicolon is required to terminate the statement.
Learning Rust Pt. 1
Rust is the system programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety. In other words, Rust is designed for speed, safety, and concurrently. You can use Rust to build the system level software. But, nowadays developers are crazy and use language other than it was intended like JavaScript.
Rustup is the recommended way to install the Rust on the computer. The installation steps are quite easy. Because of that, I do not plan to re-write steps again here. Go ahead and visit Install page to install the Rust on the computer.
After the installation is done, we'll get rustc or Rust compiler, cargo as package manager and build system, and rustup to manage Rust and related toolchain.
rustc --version rustc 1.66.0 (69f9c33d7 2022-12-12)
cargo --version cargo 1.66.0 (d65d197ad 2022-11-15)
rustup --version rustup 1.25.1 (bb60b1e89 2022-07-12) info: This is the version for the rustup toolchain manager, not the rustc compiler. info: The currently active `rustc` version is `rustc 1.66.0 (69f9c33d7 2022-12-12)`
Click here for the Next in the series.