Values in Rust
Rust has various value types including strings, integers, floats, booleans, etc. Here are a few basic examples.
To run the program, save it as values.rs
and use rustc
to compile it, then run the resulting executable:
In this Rust code:
We use the
println!
macro instead of afmt.Println
function. Theprintln!
macro in Rust is similar tofmt.Println
in 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
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.