That is, learning from C99.
- by default variables are constant. To make a variable mutable, it has to have the
mutkeyword:- otherwise it would not compile
fn main() {
// also, type inferrence
let mut foo = 'A';
foo = 'b';
}- duplicate variable definitions get marked merely as unused variables:
fn main() {
let foo = 1;
let foo = 3;
}- The exclamation mark means this is a macro.
- for printing there is also
print!andeprint!andeprintln!(for stderr)
- for printing there is also
fn main() {
println!("Hello, world!");
}- print macros have interpolation:
fn main() {
let foo = "bar";
println!("{}", foo);
println!("{foo}"); // since some Rust version
}- the ability to use expressions within the
{}seems to be limited (unlike in Python)
fn main() {
let signs = "QRYZEPTGMk ";
println!("{signs.len()}"); // fails to compile
println!("{}", signs.len());
}- no semicolon at the end of a function means the value should be returned
- in such case, the
unitprimitive type marked()is returned
- in such case, the
fn main() {
// there does not have to a semicolon for the last expression/statement in a block/function
let x = println!("{}", 'A'.is_ascii_hexdigit())
}- there is 128-bit integer type:
fn main() {
let mut foo:u128 = 2 << 68;
while foo > 0 {
// also, string interpolation
println!("{foo}");
foo = foo / 8;
}
}- accessing a slice beyond outside index results in panic (due to the bound check):
fn main() {
let signs = " TGMK";
// also note that slice cannot be used directly on str (&signs[8]) cause of UTF-8
let letter = &signs.as_bytes()[8];
}- sliced
str(string type) has to be dereferenced and casted to get a printable char:
fn main() {
let signs = "TGMK ";
let letter = &signs.as_bytes()[0];
println!("letter '{}'", *letter as char);
}-
rustcis the low-level compiler utility,cargois the high-level (make/dependencies)cargo run,cargo build,cargo clean
-
there is no macro language per se like in C, however portions of code can be compiled based on flags
# Cargo.toml
[features]
my_feature = [] #[cfg(feature = "my_feature")]
{
println!("count = {count}");
print!("{foo} ");
}