Rust – Literals
Last Updated :
08 Sep, 2022
A literal is a source code that represents a fixed value and can be represented in the code without the need for computation. The compiler uses by default i32 for integers and f64 for float types/. In Rust, literals are described by adding them in the type as a suffix.
Example: Integer literal 6 has the type i32 written 60i32 and Float literal 6.0 has the type i64 and written as 6.0i64.
To understand an example of literal, we use the std::mem:: size_of_val(&variable). This function returns the pointed reference value in bytes.
Example 1:
Rust
fn main() {
let x = 3f32;
let y = 6.0f64;
println!( "size of `x` is {} bytes" , std::mem::size_of_val(&x));
println!( "size of `y` is {} bytes" , std::mem::size_of_val(&y));
}
|
Output:
Example 2:
Rust
fn main() {
let x = 2;
let y = 5.0;
println!( "size of `x` is {} bytes" , std::mem::size_of_val(&x));
println!( "size of `y` is {} bytes" , std::mem::size_of_val(&y));
}
|
Output:
Share your thoughts in the comments
Please Login to comment...