https://github.com/sid-146/getting-rusty
https://github.com/sid-146/getting-rusty
Last synced: about 1 month ago
JSON representation
- Host: GitHub
- URL: https://github.com/sid-146/getting-rusty
- Owner: sid-146
- License: mit
- Created: 2023-06-20T17:45:57.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-17T21:12:30.000Z (about 1 year ago)
- Last Synced: 2025-02-07T16:41:09.284Z (3 months ago)
- Language: Rust
- Homepage: https://sid-146.github.io/Getting-Rusty/
- Size: 17.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Getting RustyRust is a modern, systems programming language that combines high performance with a strong emphasis on memory safety and zero-cost abstractions. What sets Rust apart is its ownership system, which enables fine-grained control over memory allocation without the need for a garbage collector. Rust is particularly well-suited for building robust and efficient systems, including operating systems, game engines, and networked applications.
Variables
`let` is used to create a variable
Syntax to declare Syntax
`let variable_name = value_to_assign`
In rust the variables are immutable by default to make them mutable `mut` keyword is used before variable name
`let mut variable_name = value_to_assign`
Sample Code for variable
```
// default variable are immutable
let x = 5;
println!("This is immutable x : {}", x);#This is immutable x : 5
// #[warn(unused_mut)] //! This will create a warning in case of variable in never updated
let mut y = 6;
println!("This is mutable y : {}", y);#This is mutable y : 6
y = 7;
println!("This is mutable y : {}", y);#This is mutable x : 7
const MY_COUNT: u32 = 100_000;
println!("This is const MY_COUNT : {}", MY_COUNT);#This is const MY_COUNT x : 100000
```### Shadowing
- Shadowing allows you to create a variable with same name
- But once the new value is assigned the old value is lost### Why shadowing
- It preserves mutability
- see i have not used mut keyword still i am able to change the value of shadow variable
- also we can change the datatype of the variable### Sample Code
```
let shadow = 10;
println!("This is int shadow : {}", shadow);#This is int shadow : 10
let shadow = shadow * 2;
println!("This is shadow : {}", shadow);#This is shadow : 20
let shadow = 10.9;
println!("This is string shadow : {}", shadow);#This is int shadow : 10.9
let shadow = "This is string";
println!("This is string shadow : {}", shadow);#This is int shadow : This is string
```
Made by ❤ sid-146