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
false

In this Rust code:

  1. We use the println! macro instead of a fmt.Println function. The println! macro in Rust is similar to fmt.Println in that it prints to standard output and adds a newline.

  2. String concatenation in Rust is done using the + operator, but the left operand must be owned (like String::from("rust")) while the right can be a string slice (&str).

  3. Arithmetic operations work similarly to other languages.

  4. Boolean operations are the same as in many other languages, including the use of && for AND, || for OR, and ! for NOT.

  5. In Rust, we don’t need to import any additional modules for basic printing or operations, unlike Go where fmt needs 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.