Values in Rust
Rust has various value types including strings, integers, floats, booleans, etc. Here are a few basic examples.
fn main() {
    // Strings, which can be added together with `+`.
    println!("{}", String::from("rust") + "lang");
    // Integers and floats.
    println!("1+1 = {}", 1 + 1);
    println!("7.0/3.0 = {}", 7.0 / 3.0);
    // Booleans, with boolean operators as you'd expect.
    println!("{}", true && false);
    println!("{}", true || false);
    println!("{}", !true);
}To run the program, save it as values.rs and use rustc to compile it, then run the resulting executable:
$ rustc values.rs
$ ./values
rustlang
1+1 = 2
7.0/3.0 = 2.3333333333333335
false
true
falseIn this Rust code:
We use the
println!macro instead of afmt.Printlnfunction. Theprintln!macro in Rust is similar tofmt.Printlnin that it prints to standard output and adds a newline.String concatenation in Rust is done using the
+operator, but the left operand must be owned (likeString::from("rust")) while the right can be a string slice (&str).Arithmetic operations work similarly to other languages.
Boolean operations are the same as in many other languages, including the use of
&&for AND,||for OR, and!for NOT.In Rust, we don’t need to import any additional modules for basic printing or operations, unlike Go where
fmtneeds to be imported.
This example demonstrates basic value types and operations in Rust, providing a foundation for understanding how the language handles different kinds of data.