Constants

The first form of global values are constants. Here's how we can define one:

// constants.rs

const HEADER: &'static [u8; 4] = b"Obj";

fn main() {
println!("{:?}", HEADER);
}

We use the const keyword to create constants. As constants aren't declared with the let keyword, specifying types is a must when creating them. Now, we can use HEADER where we would use the byte literal, Obj.  b"" is a convenient syntax to create a sequence of bytes of the &'static [u8; n] type, as in a 'static reference to a fixed-sized array of bytes. Constants represent concrete values and don't have any memory location associated with them. They are inlined wherever they are used.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset