fix(guide): simplify directory structure

This commit is contained in:
Mrugesh Mohapatra
2018-10-16 21:26:13 +05:30
parent f989c28c52
commit da0df12ab7
35752 changed files with 0 additions and 317652 deletions

View File

@@ -0,0 +1,85 @@
---
title: Rust Data Types
---
Understanding the type system of a programming language is essential for getting started with writing programs. A type system is simply a system that a programming language enforces for you to store information in data. If you are a developer coming from C/C++, some of these data types may already be familiar to you.
Let's look at Rust's primitive types
---
When we look at Rust, we see a language that places much more focus on __type-safety__. This means that interacting and handling how the programmer stores data, the compiler is much more strict and unforgiving. One example (which we will see soon) is introduced are through __warnings__.
Rust is also a statically-typed programming language. This means that when the program is compiling (also known as _compile-time_), the type of your variables must be known. Rust has the option of providing _implicit_ or _explicit_ types.
### Integers
Let's see how _implicit_ or _explicit_ types work by declaring a simple integer.
let number = 13;
We declared our variable, `number` to equate to the value of 13. When we compile this, Rust knows that the type of the number is `i32`, also known as a 32-bit integer. If we wanted to do this _explicitly_, we can declare the variable as so:
let number: i32 = 13;
Of course, `i32` is not the only type that we can specify. Here a table of the other `integer` types that you can implement. Keep in mind that these are _fixed-sized_, and the number of bits corresponding with the type is the number of bits that can be stored in that variable.
| Size | Signed | Unsigned |
| ----- | ---- | --- |
| 8-bit | i8 | u8 |
| 16-bit | i16 | u16 |
| 32-bit | i32 | u32 |
| 64-bit | i64 | u64 |
Signed (`i` prepend) and unsigned (`u` prepend) represent whether or not the variables that we declare are positive or negative, respectively.
Rust also has the `isize` and `usize` type, which will depend on your computer's architecture. If you are on a 64-bit architecture, you will have a 64-bit variable. If you are on a 32-bit architecture, you will have a 32-bit variable.
### Floats
Rust also has support for float values, or decimal values.
let floaty = 3.0; // type is f64
The variable `floaty` was implicitly declared to be a `f64` type, or 64-bit float. If you want a `f32`, you can coerce a variable by either being explicit, OR appending the type to end of the variable value:
let float1: f32 = 4.5; // type is f32
let float2 = 5f32; // type is f32
You can see that `float2` is now a `f32`, even though it has a non-decimal value. By appending a `f32`, we coerced `float2` to represent a `f32` type.
## Booleans
In every programming language, booleans values (`true` and `false`) are always present in some form. Rust has support for this through the `bool` type.
let t = true;
let f: bool = false;
## Numerical Operations
With that said, we can now use Rust to perform simple numerical operations. Notice how types are restricted to calculations only with their own types.
let a = 4 + 5; // => 9
let b = 5.0 - 4.0; // => 1.0
let c = 3 * 4.0; // => ERROR!
let d = 6.0 / 2.0; // => 3.0
let e = 10 % 4; // => 2
## Warnings!
Let's look at some of the potential problems that can already be present in Rust. Since utilizing data efficiently and making sure that the variables that we create are actually being used, Rust will throw warnings for unused variables:
fn main(){
let a = 4;
// and now we close our program.
}
Now if we run this program:
warning: unused variable: `a`
It's important to remember to always use our variables, and if not, comment them out or remove them!
## Read more:
https://doc.rust-lang.org/book/second-edition/ch03-02-data-types.html

View File

@@ -0,0 +1,47 @@
---
title: Hello World
---
## Hello Rust
Writing your first Rust program is as easy as installing it. In the project directory of your choice, create a new source file called `main.rs`. It is important to note that Rust files always end with the `.rs` extension and filenames with more than one word are separated with underscores. For example, `helloworld.rs` would become `hello_world.rs`.
After creating `main.rs`, add the following code within:
```rust
fn main() {
println!("Hello, world!");
}
```
Wow! That was easy, wasn't it? Within you're new `main.rs` file, the following is true:
+ The first line `fn main()` denotes a _function_ in Rust. The `main` function is special, it is the first thing that is called for every executable Rust program.
+ The second line `println!("Hello, world!")` is calling a Rust _macro_, passing a _string_ as it's first argument. This line prints the string "Hello, world!" to the terminal. You can tell if you are calling a Rust _macro_ or _function_ through observation of the `!`.
To execute the program, you must first compile it:
```bash
$ rustc main.rs
```
This process will create an executable in the same directory, which you can then run:
```bash
$ ./main
Hello, world!
```
Congratulations! You've just written your own Rust program!
## Hello Cargo
Cargo is the build tool that gets shipped with Rust when you installed it and can be used for many things. Here we will see the alternative approach of using cargo.
Firstly navigate to the parent directory of your project and run `cargo new hello_world`. This will create our project directory of `hello_world` with some files inside it, those being `Cargo.toml` which tells `cargo` how to build your project, as well as `src/main.rs` which is our Rust source file. If you open this file you will see some code is already generated for us to run hello world! So let's do that.
To run your program with cargo it is as simple as running `cargo run` in your project directory and it should look something like this:
```bash
cargo run
Compiling hello_world v0.1.0
Finished dev [unoptimized + debuginfo] target(s) in 1.31s
Running `target/debug/hello_world`
Hello, world!
```
Great work you now have even more tools at your disposal!

View File

@@ -0,0 +1,50 @@
---
title: Rust
---
# Rust
## Introduction
Rust is a systems programming language focused on three goals: safety, speed, and concurrency. Its design lets you create programs that have the performance and control of a low-level language, but with the powerful abstractions of a high-level language. These properties make Rust suitable for programmers who have experience in languages like C and are looking for a safer alternative, as well as those from languages like Python who are looking for ways to write code that performs better without sacrificing expressiveness. Rust runs the majority of its safety checks and memory management decisions at compile time, so that your programs runtime performance isnt impacted. This makes it useful in a number of use cases that other languages arent good at: programs with predictable space and time requirements, embedding in other languages, and writing low-level code, like device drivers and operating systems. Also used for web applications also powers the Rust package registry site, <a href='https://www.crates.io' target='_blank' rel='nofollow'>crates.io</a>.
For more information head to <a href='https://www.rust-lang.org' target='_blank' rel='nofollow'>Rust's Homepage</a>.
## Installation
The developers of rust make it extremely easy to install and manage rust on your system. This is achieved through the tool `rustup` which allows you to not only install the rust compiler `rustc`, but also easily switch between stable, beta, and nightly versions of the compiler and keep them all up to date.
The official installation documentation can be found [here](https://doc.rust-lang.org/book/second-edition/ch01-01-installation.html).
### Linux or Mac
If you're running Linux or Mac, installation of `rustup` is best done through the terminal:
```bash
$ curl https://sh.rustup.rs -sSf | sh
```
This will download and run a script to your machine that installs the tool. The installation script automatically adds Rust to your system `PATH` after your next login.
### Windows
On Windows, go to the [rustup website](https://rustup.rs) and follow the instructions to download `rustup-init.exe`. Run that and follow the rest of the instructions it gives you.
### Updating
Once you have installed `rustup`, updating to newer versions is simple. All you need to run is:
```bash
$ rustup update
```
To view the current version number, commit hash, and commit date of your rust compiler, run the following command:
```bash
$ rustc --version
rustc x.y.z (abcabcabc yyyy-mm-dd)
```
### Uninstalling
Uninstalling rust from your system is as easy as installing it:
```bash
$ rustup self uninstall
```

View File

@@ -0,0 +1,36 @@
---
title: Installing Rust
---
# Installing Rust
Using `rustup` is preferred for Rust installation. `rustup` installs and manages Rust for your system.
## Installing Rust in Windows
Visit the [rustup website](https://rustup.rs) and download the `rustup-init.exe`. Install it and then you should be ready to go!
## Installing Rust in other operating systems (Mac OS X, Linux, BSD, Unix)
Open up your terminal and type in this command:
```sh
curl https://sh.rustup.rs -sSf | sh
```
This will fetch the `rustup` installer and in turn fetch everything you need.
# Verifying installation
Installing `rustup` will install all things relevant to rust, but most relevantly this means installing the compiler and the package manager. To verify that everything is installed, run this command:
```sh
cargo version
```
You will now be able to use Rust!
# More information
To learn more about the install process, visit
https://www.rust-lang.org/en-US/install.html

View File

@@ -0,0 +1,55 @@
---
title: Loops
---
# Loops
Within Rust there are three kinds of native looping mechanisms: `loop`, `while`, and `for`.
## Infinite repetition with `loop`
In Rust, the `loop` structure will continually execute a block of code ad infinitum, (or until you explicitly tell it to stop).
Here is an example program using `loop` to print the word 'again' continually to the terminal:
```rust
fn main() {
loop {
println!("again!");
}
}
```
## Conditional looping with `while`
The above mechanism is not very useful unless we introduce some kind of stopping condition for the `loop` to check for. Luckily, Rust has an in-built looping structure called `while`, that you can use to continually execute a block of code whilst some condition is true.
Here is an example program using `while` to count down from 5:
```rust
fn main() {
let mut number = 5;
while number != 0 {
println!("{}", number);
number = number - 1;
}
}
```
Run the code [here](https://play.rust-lang.org/?gist=62677371a8590be27c84dcae7068de57&version=stable).
## Iterating through a collection with `for`
In some instances, you might want to iterate and operate on the elements of a collection (such as an array). Whilst you could achieve this using a `while` loop and an index variable to access each element, Rust provides the `for` loop to make this operation much easier.
Here is an example program that prints each number in an array to the terminal using `for`:
```rust
fn main() {
let collection = [15, 7, 2, 6, 9];
for element in collection.iter() {
println!("the value is: {}", element);
}
}
```
Run the code [here](https://play.rust-lang.org/?gist=0c2acf21b96a81ebd411e4a7dc5a19fd&version=stable).
Much like iterators in C++, the `.iter()` function returns an iterator to the `collection`, which can then be looped through to access each `element`. For more information, head to the Rust documentation on [control flow](https://doc.rust-lang.org/book/second-edition/ch03-05-control-flow.html).