Rust Tutorial | Rust Programming Language Tutorial For Beginners | Rust Training | Edureka
The document provides an overview of the Rust programming language, highlighting its key features such as memory safety, concurrency, and zero-cost abstractions. It covers Rust fundamentals including variables, constants, data types, functions, control flow structures, ownership, and modules with relevant syntax examples. Additionally, it discusses compound data types like arrays and tuples, and introduces the concept of vectors for storing multiple values.
Overview of the presentation agenda and reasons to learn Rust programming language.
Highlights advantages such as Cargo package manager, concurrency, memory safety, zero-cost abstraction, and statistics that underline Rust's popularity.
Defines Rust, emphasizing efficient C binding, memory safety, error messages, and its development origin.
Details the concept of variables and constants in Rust programming, including syntax for defining mutable variables.
Explains scalar and compound data types, including arrays, tuples, and strings, with syntax examples.
Describes functions as reusable code blocks with examples including predefined and user-defined functions.
Introduces conditional statements in Rust, specifically 'if', 'if-else', and 'else if' structures, with syntax.
Details looping mechanisms including general loops, for loops, and while loops with illustrative syntax.
Explains ownership concept in Rust, its rules, and an example demonstrating variable ownership.
Describes structures as user-defined data types with varied data types, including syntax for definition.
Introduces enums as custom data types containing definite values, including their syntax.
Explains modules as namespaces in Rust, detailing their role in organizing functions and types.
Discusses vectors as a data structure for storing multiple values, emphasizing their characteristics and syntax.
What is Rust? Zerocost abstraction Efficient C binding Ensures memory safety Pattern matching Error messages Threads without data races Rust is a safe, concurrent, systems programming language developed by a Mozilla employee, Graydon Hoare in 2006.
Compound Data Types- Arrays An array is a data structure that contains a list of elements. These elements are all of the same data type, such as an integer or string. Syntax: 1 2 let arr=[ ]; 0 1 2 3 4 5 6
13.
Compound Data Types- Tuple A tuple is a data structure that contains a list of elements. These elements can be of different data type, such as an integer or string. Syntax: 1 2 let tup: (i32, f64, u8) = (200, 1.2, 2); 0 1 2 3 4
14.
Strings A string isbasically an array of characters. Syntax: 1 2 let mut s = String::new( ); E D U R E K A 0 1 2 3 4 5 6
15.
Functions A function isa block of organized, reusable code that is used to perform a single, related action. Functions Predefined Functions User Defined Functions Syntax: 1 2 3 fn greetings( ) { println!(“Hello everyone!”); } name of the function enter parameters here
16.
Conditional Statements -If Conditional statement is a set of rules performed if a certain condition is met. It is like an ‘If-Then’ statement. (IF a condition is met, THEN an action is performed) If code End Start True Condition False Exit Syntax: 1 2 3 if(condition) { statement; }
17.
Conditional Statements –If else Conditional statement is a set of rules performed if a certain condition is met. It is like an ‘If-Then’ statement. (IF a condition is met, THEN an action is performed) If code End Start True Condition False Else if code Syntax: 1 2 3 4 5 6 if(condition) { statement a; } else { statement b; }
18.
Conditional Statements –Else if Conditional statement is a set of rules performed if a certain condition is met. It is like an ‘If-Then’ statement. (IF a condition is met, THEN an action is performed) If code End Start True Condition False Else if code Syntax: 1 2 3 4 5 6 7 8 9 if(condition) { statement a; } else if(condition) { statement b; } else { statement c; }
19.
Loop Loop is usedto repeat a specific block until some end condition is met. Start Conditional Code Condition False True End loop Syntax: 1 2 3 loop { statements; }
20.
For Loop Repeatedly executesthe loop code while a given condition is TRUE. It tests the condition before executing the loop body. Start Execute Statement (s) End Next item from sequence If no more itemsItem from sequenceSyntax: 1 2 3 4 for var in expression { block statements; }
21.
While Loop While thecondition is true, the code within the loop is executed. Start Conditional Code End True False Condition Syntax: 1 2 3 while(condition) { loop code; }
22.
Ownership When a blockof code owns a resource, it is known as ownership. • Every value has a variable associated which is the owner • There can only be one owner at a time • If the owner goes out of scope, the value associated with it is destroyed RULES Example: 1 2 let x=12; let y=x;
23.
Structure A structure isa user-defined data type that consists of variables of different data types. Syntax: 1 2 3 4 5 struct Student { variable: data-type; variable: data-type; } Name Age Father’s name
24.
Enum Enum is acustom data type which contains some definite values. Syntax: 1 2 3 4 5 enum enum_name { variant1, variant2, } Stop Ready Go Traffic Signal
25.
Modules A module isa namespace which contains the definitions of the functions or its types. A module is a collection of items such as functions, traits, structs, blocks. Syntax: 1 2 3 mod module_name { module body } • mod - declares the new module • pub - makes the visibility modifier as public & accessible outside the namespace • use - to import the module into local scope Keywords used in modules:
26.
Vectors Vectors allow youto store more than one value in a single data structure that puts all the values next to each other in memory. Syntax: 1 2 let v : Vec<i32> = Vec::new( ); • Stores values of the same data type • It is denoted by Vec<T> • It is a growable array Key points of Vectors: