Rust Modules

let us start creating a rust file with the module name as a new module.

 

mod newmodule;

fn method() {
println!("called `method()`");
}

fn main() {
newmodule::method();

method();

newmodule::indirect_access();


}

 

newmodule file is created as shown below:

 

pub fn method() {
println!("called `newmodule::method()`");
}

fn private_method() {
println!("called `newmodule::private_method()`");
}

pub fn indirect_access() {
print!("called `newmodule::indirect_access()`, that\n> ");

private_method();
}


 

The rust module.rs is compiled and the output is as shown below:

apples-MacBook-Air:modules bhagvan.kommadi$ rustc moduleaccess.rs

apples-MacBook-Air:modules bhagvan.kommadi$ ls

moduleaccess moduleaccess.rs newmodule.rs

apples-MacBook-Air:modules bhagvan.kommadi$ ./moduleaccess 

called `newmodule::method()`

called `method()`

called `newmodule::indirect_access()`, that

> called `newmodule::private_method()`

Square Area: Struct & methods

let us look at area of a square  in rust using struct & methods

main.rs

use std::fmt;

struct Square {
    side: i32
}

impl fmt::Display for Square {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Square of side {}", self.side)
    }
}
impl Square {

fn area(&self) -> i32 {

   let x = self.side;
   x*x
 }
}

fn main() {
    let square = Square { side: 4 };
    println!("{}", square.to_string());
    
     println!("Square area: {}", square.area());
}


Cargo.toml

[package]
name = "rust_square"
version = "0.1.0"
authors = ["bhagvan.kommadi"]
edition = "2018"

[dependencies]

 

Run the following command:

cargo run

 

The screenshot of the output will look like this:

 

rust_square_area

Rust : Get Sum of two numbers

started with Getting the sum of two numbers in rust

main.rs

fn main() {
  let a      = 15;
  let b: i32 = 25;

  let c = get_sum(a,  b);
  println!("a + b = {}", c);
}

fn get_sum(i: i32, j: i32) -> i32 {
  i + j
}

 

Cargo.toml

[package]
name = "rust-sum"
version = "0.1.0"
authors = ["bhagvan.kommadi"]
edition = "2018"

[dependencies]

 

Run the following command:

cargo run

 

The screenshot of the output will look like this:

 

rust_sum

Rust 1.39.0

You can check out the release notes of Rust 1.39.0 on github .

Key features

 

  • include async/.await,
  • shared references to by-move bindings in match guards
  • attributes on function parameters.

 

The  functions mentioned below are const fn:

  • Vec::new, String::new, and LinkedList::new
  • str::len, [T]::len, and str::as_bytes
  • abs, wrapping_abs, and overflowing_abs

rust_image

 

 

Rust – Early Adopter

 

Rust is being widely adopted at this point among San Francisco Bay Area companies,.  The adoption is low in other areas. Rust has moved from innovator to early adopter quadrants. This is due to its uptake within the infrastructure and networking data plane space. Habitat and Linkerd 2.0 have adopted Rust as their core platform. It is emerging as a natural partner for WebAssembly. This is helping to drive awareness. It is also important that  Facebook has chosen to implement its Libra cryptocurrency using Rust.

 

“Rust definitely seems to have seen growth in the last year, and I’d move it from innovator to early adopter. They’ve been good at positioning it as a partner with Wasm, which is helping, I think.” –  Charles Humble, Editor in Chief at InfoQ: