SkillRary

Please login to post comment

INTRODUCTION TO RUST

  • Amruta Bhaskar
  • Jan 27, 2020
  • 0 comment(s)
  • 2924 Views

Rust is a multi-system a programming language that concentrates basically on safety and safe concurrency. It is similar to C++ syntactically, but it is designed to provide memory safety in a better way while maintaining high performance.

It was designed by Graydon Hoare at Mozilla Research, supported by the contributions from Dave Herman, Brendan Eich and many others. While writing the servo layout or browser engine and the rust compiler, the language was refined by the designers and it is the most loved programming language since 2016 in the stack overflow Developer Survey every year. The compiler is an open-source software dual licensed and it is free under MIT License and Apache License 2.0.

Rust for high concurrency and high safety systems in large programming like creating and it also maintains boundaries so that preserves large-system integrity which led to the features concurrency, safety and memory layout control.

The syntax is similar to C and C++. The blocks of code are defined by curly braces, control flow keywords like if, else, while, for etc. In rust, not all C or C++ keywords are implemented. Despite resembling C and C++, the syntax in a broader sense is close to languages of ML family and Haskell language. Every part of the function body is an expression.

For example, the if expression also takes the place of ternary condition in C. A function need not to end with return statement, here if the semicolon is excluded, the last expression creates the return value in the function.

It does not allow the null pointers, dangling pointers or data races in safe code as it is designed to be memory safe. The data values are initialized through a fixed set of forms that require their input to be already. Rust introduces the added syntax to manage lifetimes and compiler reasons about there through it borrows checker. It does not use an automated garbage collection system but the memory and other resources are handled through resource acquisition is initialization (RAII) convention.

Types and polymorphism

The type system here supports a mechanism that is similar to type classes called traits. It is directly inspired by the Haskell language. This is basically a facility for the ad-hoc polymorphism that has been achieved by adding constraints to the type variable declarations. The feature type interference, for the variables, declared with the keyword “let”, those variables do not require a value to be assigned initially to determine the type. If any branch of code fails to assign a value to the variable, it results in a compile-time error. The keyword “mut” must be used when the variable is assigned multiple times.

In RUST the functions can be given generic parameters, that require the generic type to implement a certain trait/traits so that the generic value can be used through those traits, it means that generic function can be type-checked as soon as it is defined. This is in contradiction with C++ templates, that are fundamentally duck typed and it cannot be checked until instantiated with concrete types. However, the rust generic implementation is similar to that of the C++ templates implementation, here a separate copy of code is generated for each instantiation and is called as monomorphization.

The object system within the rust is based on implementations, traits and structured types. Here the implementations carry a role that is similar to the classes within other languages and is defined with impl keyword.

Example

Here is a sample of a program written in rust. Here the println! macro prints the message to the standard output.

            fn main()       {

                        println!(“Hello World”);

            }

Steps to create, compile and run the program in RUST

·        Open the notepad and write the code.

·        Save the file with .rs extension.

·        Open the command prompt.

·        Set the path of the directory.

·        Compile the program using the rustc command.

suppose if the file name is hello.rs, compile it using rustc hello.rs command

·       Finally, run the program using the command filename.exe.

 

 

Author: Vindhya S

Please login to post comment

( 0 ) comment(s)