first commit
This commit is contained in:
commit
a66604eb6c
52 changed files with 790 additions and 0 deletions
7
datatypes/Cargo.lock
generated
Normal file
7
datatypes/Cargo.lock
generated
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 4
|
||||
|
||||
[[package]]
|
||||
name = "datatypes"
|
||||
version = "0.1.0"
|
||||
6
datatypes/Cargo.toml
Normal file
6
datatypes/Cargo.toml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
[package]
|
||||
name = "datatypes"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
12
datatypes/src/general.rs
Normal file
12
datatypes/src/general.rs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
pub fn static_variables() {
|
||||
//integer
|
||||
let a: i32 = 4;
|
||||
//float
|
||||
let pi: f64 = 3.14;
|
||||
//boolean
|
||||
let iscorrect: bool = true;
|
||||
//char
|
||||
let c: char = 'A';
|
||||
println!("{}:{}:{}:{}", a, pi, iscorrect, c);
|
||||
}
|
||||
|
||||
22
datatypes/src/hashmap.rs
Normal file
22
datatypes/src/hashmap.rs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
use std::collections::HashMap;
|
||||
pub fn hashmapf() {
|
||||
/*
|
||||
in rust, HashMap are the data types which can store according to the key value pair
|
||||
*/
|
||||
let mut scores = HashMap::new();
|
||||
scores.insert("blue", 10);
|
||||
scores.insert("red", 100);
|
||||
|
||||
println!("Blue: {:?}", scores.get("blue"));
|
||||
|
||||
match scores.get("red") {
|
||||
Some(x) => println!("Red => {}", x),
|
||||
None => println!("error"),
|
||||
}
|
||||
|
||||
//for removing
|
||||
scores.remove("blue");
|
||||
for (key, value) in &scores {
|
||||
println!("{} => {}", key, value);
|
||||
}
|
||||
}
|
||||
12
datatypes/src/main.rs
Normal file
12
datatypes/src/main.rs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
mod general;
|
||||
// mod string_array;
|
||||
// mod tuples;
|
||||
// mod vectors;
|
||||
//mod hashmap;
|
||||
fn main() {
|
||||
general::static_variables();
|
||||
// string_array::s_a();
|
||||
// tuples::tuples();
|
||||
// vectors::vectors();
|
||||
// hashmap::hashmapf();
|
||||
}
|
||||
26
datatypes/src/string_array.rs
Normal file
26
datatypes/src/string_array.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
pub fn s_a() {
|
||||
//rust had two main string types
|
||||
// 1. &str -> String slice which is immutable and usually used when reading static strings or string litreals
|
||||
let str_slice: &str = "safal lama";
|
||||
println!("string slice: {}", str_slice);
|
||||
|
||||
//2. String-> which is mutalbe and used when you need to modify or build strings at runtime
|
||||
let mut str_val = String::from("safal");
|
||||
str_val.push_str("ski");
|
||||
println!("general String: {}", str_val);
|
||||
|
||||
|
||||
//array-> is fixed size of same data type => stord on stack
|
||||
let arr = ['a','b','c','d'];
|
||||
println!("{:?}", arr);
|
||||
println!("{}", arr[0]);
|
||||
|
||||
for i in 0..arr.len(){
|
||||
print!("{}, ", arr[i]);
|
||||
}
|
||||
println!();
|
||||
for i in arr{
|
||||
print!("{}, ", i);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
14
datatypes/src/tuples.rs
Normal file
14
datatypes/src/tuples.rs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
pub fn tuples() {
|
||||
/*
|
||||
tuples are the data structure that can store multiple values of different types
|
||||
*/
|
||||
|
||||
let my_tup = (42, 3.14, 'R');
|
||||
println!("{:?}", my_tup);
|
||||
println!("{}", my_tup.0);
|
||||
|
||||
//tuples can be destructured
|
||||
let (x, y, z) = my_tup;
|
||||
println!("destructured: x={}, y = {}, z ={}", x, y, z);
|
||||
|
||||
}
|
||||
29
datatypes/src/vectors.rs
Normal file
29
datatypes/src/vectors.rs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
pub fn vectors() {
|
||||
/*
|
||||
in rust vectors Vec<T> are growable, heap allocated arrays that store elements of the same
|
||||
type. they are part of standard library and are commonly used when you need a dynamically
|
||||
sized array.
|
||||
[features]
|
||||
- vectors are grow or shrink in sized
|
||||
- elements are stored contiguiously in memory
|
||||
- they may useful in Dref, index, Intollerator ets traits
|
||||
*/
|
||||
|
||||
//creating a vectors
|
||||
let mut v: Vec<i32> = Vec::new(); //empty one[must be have to type anotate explictly]
|
||||
let v1 = vec![1, 2, 3, 45];
|
||||
println!("{:?}", v1);
|
||||
|
||||
//adding elements
|
||||
v.push(12);
|
||||
println!("{:?}", v);
|
||||
|
||||
//accessing elements specific
|
||||
println!("{}", v1[0]);
|
||||
|
||||
//for fore safety like if index id out of bound to graceful handle stuff we use match
|
||||
match v1.get(2) {
|
||||
Some(value) => println!("value: {}", value),
|
||||
None => println!("no value found at that index"),
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue