Posts

Rust Hello World Program

Image
  In Rust Hello World Program , I have introduced Rust programming. We will learn about the basics of Rust programming in this article. The first line starts with //, depicting this line as a comment. A comment is documentation for another developer. We can also start a comment after the code. But all comments end at the end of the line.     println ! ( "Hello, world!" ) // This line prints Hello, world! In every programming language, a program is a sequence of instructions for the processor to execute. We also need a starting point where the instruction starts executing. In Rust programming, there is a special function named the main where the execution of instructions starts. When all instructions in this function including the calls to other functions end, the program ends. After reading Programming Concepts Part III and this article together with the YouTube video , You might be willing to learn more about programming. Last but not least, every program...

Borrowing In Rust

Image
In this article, we will learn about borrowing in Rust. In Ownership In Rust , we have learned that only a single variable can hold the ownership of data on the heap at a time. Before starting the discussion of borrowing, we should understand that assigning a variable to another variable, passing a variable as an argument to a function, and assigning the return value of a function to a variable are the same. Another concept of scope is also very important. In Rust, the last use of a variable defines the scope of the variable. In other words, the scope of a variable ends when it is used the last time in a function. When we assign a variable that points to data on the heap to another variable, it transfers ownership of the data to another variable. As we have read above, ownership is also transferred in the case of an argument passing to a function and returning a value from the function. To get rid of the situation, where ownership is transferred to a function and again re...

Ownership In Rust

Image
    In this article, we will learn about ownership in Rust. fn main () {     let i = 5 ; //on stack     let j = i ; //on stack     println! ( "i = {i}, j = {j}" );       let str = String :: from ( "theloveoftechI" );     let str1 = str . clone ();     println! ( "{str}, {str1}" );       let mv_str = str ;     println! ( "{mv_str}" );     //println!("{str}"); //This is problematic }   Variables are allocated on the stack or on the heap. We can only push an item on the top of the stack. We can also pop an item from the top of the stack. Variables on the stack have fixed sizes. All variables are allocated on the stack when a function is called. In the case of a heap, space is allocated based on the size of the data. If we want to grow a particular vegetable in the garden, so we reserve a certain area for that vegetable. In the case of i and j, both are on the stack ...

Nested Tuple In The Rust Language

Image
Please watch this video before reading the content below.   In this article, we will learn about de-structuring of a tuple in the Rust programming language.  let my_tuple = (9, "nine", 9.0); A tuple can contain elements of different types, but once created, it can not be changed. We can access its first element as my_tuple.0 and the second as my_tuple.1 and so on. Another way is called de-structuring, let (num, num_str, num_float) = my_tuple; Now, we can access the first element of my_tuple as num and the second as num_str and so on. Now, we will consider the situation when one tuple is nested inside the other tuple. let nested_tuple = (9, ("nine", 9.0)); The first element of the nested_tuple is accessed as usual nested_tuple.0, but the second element is itself a tuple. So, (nested_tuple.1).0 is "nine" and (nested_tuple.1).1 is 9.0. Now, de-structuring works as follows,  let (num, (num_str, num_float)) = my_tuple; One unique characteristics I've notic...

Programming Concepts Part III

  Continuing from Programming Concepts Part II . A computer cannot understand instructions as given in the previous part. We have to change those human-understandable instructions to machine-understandable instructions. It is known as the machine language of the computer. A computer only understands a sequence of 0s and 1s. Most of the time we see mention of the 0 and the 1. But we can also say yes/no, on/off, true/false and so on. There are two states, either present or absent. All numbers, texts, images and so on are a sequence of 0s and 1s. There are two states or symbols(0 and 1), so it is called a binary system . A single 0 or 1 is called a bit for a binary digit. Its counting goes like this, 1 = 1 10 = 2 11 = 3 100 = 4 101 = 5 110 = 6 111 = 7 1000 = 8 1001 = 9 1010 = 10 1011 = 11 1100 = 12 1101 = 13 1110 = 14 1111 = 15 and so on. The place value is like 1, 2, 4, 8, 16, 32 and so on from right to left. When the number of bits is large, it is grouped in 3 bits from the right ...

Programming Concepts Part II

Continuing from Programming Concepts Part I , now we talk about real programming tasks.   As we need containers to put items in a kitchen. Then we use these items to prepare food for ourselves. The preparation of food involves some activity. In the same manner, in programming, we need variables to store values and we do some activity to complete a task. We use instructions to complete a task. For example, we need two variables to store values and addition instruction (+) to find the sum of the two variables. In a high-level programming language, we say instructions as operators . a = 5 b = 8 a + b is 13   The first time when we introduce a variable in a program it is called the declaration of the variable. And the first-time value assigned to the variable is called initialization of the variable.   Each programming language has its own way of variable declaration and initialization. In this series, we are going to learn the fundamentals of computer programming. Langua...

Programming Concepts Part I

 In this series, we are going to learn the fundamentals of computer programming. Language-specific features will be provided in a different series.   A programming language is one of the possible ways to instruct a computer. We try to solve a real-life problem. Every programming language might not be suitable for the current problem at hand.         In other words, we can say that programming is the skill of implementing an algorithm in a programming language. An algorithm is a step-by-step solution for a problem. So, we can not solve a moderate-size problem in one step. We need to break down a problem into problems of a manageable size. The person who does programming is known as a programmer. Some programmers deal with computer hardware and they are known as system programmers. Others are known as web programmers and write web applications. So, the different types of developers(programmers) use different programming languages to handle thei...