Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
fn main() {

    //Creates the tuple x and makes new variables 
    //for each element by using their respective indices
    let x: (i32, f64, u8) = (500, 6.4, 1); 

    //The first element starts from index 0 similar to other programming languages
    let five_hundred = x.0; 

    let six_point_four = x.1;

    //The syntax to access an index of a tuple is 'tuple''period''index' as shown
    let one = x.2;
}

Rust Usage and Rust vs C

Rust can be used for topics such as operating system development, web services, command-line tools and others. Rust is an ideal programming language for those who seek efficiency and security. Rust is also very good for concurrency and memory safe. On the other hand, the C compiler does not necessarily care about safety, so programs need to be carefully written so that it doesn’t cause memory violation or data races. As for code performance, Rust is similar to C in terms of its efficiency, and it also offers the same low-level control as C. Essentially, Rust is similar to C in many aspects but there won’t be intensive debugging due to a segmentation fault caused by a memory leak.

Example of memory safety in Rust:

In Rust, memory is managed through a system of ownership with a set of rules that the compiler checks at compile time. The ownership rules are:

  • Each value in Rust has a variable that’s called its owner.

  • There can only be one owner at a time.

  • When the owner goes out of scope, the value will be dropped.

Code Block
languagerust
//The following code is taken from 
//https://doc.rust-lang.org/stable/book/ch04-01-what-is-ownership.html
//there are more examples about ownership and borrowing on this wesbite.
fn main() {
    let s1 = gives_ownership();         // gives_ownership moves its return
                                        // value into s1

    let s2 = String::from("hello");     // s2 comes into scope

    let s3 = takes_and_gives_back(s2);  // s2 is moved into
                                        // takes_and_gives_back, which also
                                        // moves its return value into s3
} // Here, s3 goes out of scope and is dropped. s2 was moved, so nothing
  // happens. s1 goes out of scope and is dropped.

fn gives_ownership() -> String {             // gives_ownership will move its
                                             // return value into the function
                                             // that calls it

    let some_string = String::from("yours"); // some_string comes into scope

    some_string                              // some_string is returned and
                                             // moves out to the calling
                                             // function
}

// This function takes a String and returns one
fn takes_and_gives_back(a_string: String) -> String { // a_string comes into
                                                      // scope

    a_string  // a_string is returned and moves out to the calling function
}

Functional Programming with Rust - OOP:

Overview of C++:

Many people know C++ since it is essentially an extension of the C language. C++ compromises a combination of both high-level and low-level language features.

...