0
47
5:13 PM

Rust Programming language: Some important things to know.

Feb 5th 235 Minutes

Rust Programming language: Some important things to know.

I'm writing this while learning Rust, so everything I write here is the basic things someone starting to learn rust should know. So let's start, we a history.

Rust History

Graydon Hoare, a Research employee at Mozilla started Rust as a personal project in the year 2006. Then later, Mozilla began sponsoring the project in 2009 as a part of the ongoing development of an experimental browser engine called Servo, then announced in 2010. The first release was on May 15th, 2015.

List Of Basic Rust Knowledge

  • The `main` function is the `beginning` of every `Rust program.`
 fn main(){}
  • In Rust, we're not able to print numbers or variables using `println!()`, but in other languages it is possible.
fn main(){
//Dont do this
let x = 5;
println!(x);
//This will throw an error.

//Do this instead.
 println!("{}", 1); 
 // or
 println!("{}", x);
 
 //Output is 1 or 5 for the variables x.
}
  • These `{}` are called `placeholders` in Rust.
  • In Rust, Positional Arguments specify the position of a value in a string. In positional arguments, each value is assigned a number based on the order of occurrence. So in the example, the first value is assigned 0, and the next is 1, so in a case where we value a third value, we will put 3 inside the placeholder if we want to display that data. Note that this is not a `Named Argument,`
fn main(){
  println!("Becoming the in the {0} is very important. So be {1}.", "industry", "patient");

  //output
  // Becoming the in the industry is very important. So be patient.  
}
  • `Named Arguments` take a name that stores a value. It is like declaring a variable to accumulate a value (string or numbers). So it works by specifying a name within the placeholder and assigning that name to a value, to be inserted inside the string in the placeholder, then displaying the output.
fn main(){
  println!("{blog_name} provides {type} needed.", blog_name="devBox", type="informations");
  
  //output
  //devBox provides informations needed.
}
  • Placeholder is Rust, have different ways to print out data to the console or uses. The placeholder used for converting values to binary, hexadecimal, and octal, is called `Placeholder Traits` and are in these formats "Binary `{:b},` Hexadecimal `{:x}`, Octal `{:o}`. Then we have a placeholder format for debugging, which is called Debug Trait. With it, it is possible to display multiple values to the console, using a placeholder with a colon followed by a question mark `{:?}`
  • Printing in Rust is done using these macros, `print! ()`, which prints strings to the console. `println! ()`, which prints strings to the console, but appends a new line character at the end of the string. `eprint! ()`, prints any string inside the parentheses as an error. And the last one `eprintln! ()` print string as errors but also appends a new line character at the end.
fn main(){
  //---print!()---
  //The following example prints “Rust Programming is fun, right?John is coding in Rust, cool right?” in one line.
  
    print!("Rust Programming is fun, right?");
    print!("John is coding in Rust, cool right?");
    
  //output Rust Programming is fun, right?John is coding in Rust, cool right? -- same line
  
  
  // ---println!()---
  //The following example prints “Rust Programming is fun, right?” on one line and “"John is coding in Rust, cool right?” on the new line.
  
    println!("Rust Programming is fun, right?");
    println!("John is coding in Rust, cool right?");
    
    /*
    output:
    Rust Programming is fun, right? --line
    John is coding in Rust, cool right? --another line
    */
    
  //---eprint!()---
  //The following example prints “Rust Programming is fun, right?” and “John is coding in Rust, cool right?” on the same line but as an error.
    eprint!("Rust Programming is fun, right?");
    eprint!("John is coding in Rust, cool right?");
    
     //output Rust Programming is fun, right?John is coding in Rust, cool right? -- same line
     
  // ---eprintln!()---
  //The following example prints “Rust Programming is fun, right?” as an error and appends a new line to it. Then prints “John is coding in Rust, cool right?” and appends a new line.
  
    println!("Rust Programming is fun, right?");
    println!("John is coding in Rust, cool right?");
    
    /*
    output as errors:
    Rust Programming is fun, right? --line
    John is coding in Rust, cool right? --another line
    */
    
}
  • `Comments` are readable instructions that help explain codes. But note that, the compiler ignores them. Rust has three types of comment, but two are commonly used. These comments are the Line Comments (`//`), `Block Comments(`/**/`) and Doc Comments. Now the Doc Comments has a sub-type called Outer Doc Comments "///" and Inner Doc Comments (`//!`)
 /// Outer Doc Comment

fn main() {
    // Line Comments
    //Example:
    //let mut x = 5;
    
    /*
    Block Comment
    Example:
    You can use the block comment when you want to disable a lengthy blocks of code
    let mut x = 5;
    */
    
   //! Inner Doc Comments
}
  • The Character in Rust, known as `char`, is used to store a single alphabet, emojis, or Korean characters. And The value assigned to a char variable is enclosed in a single quote`''`. But note that, unlike some other languages, a character in Rust takes up 4 bytes rather than a single byte. It does so because it can store a lot more than just an ASCII value like emojis, Korean, Chinese, and Japanese characters.
fn main(){
  let char_letter = 'a';
  println!("{}", char_letter)

  // output: a
}

Reach out

Resource

0 comment
Be the first to leave a comment