summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoranand <anand.panchdhari@gmail.com>2026-04-06 22:33:59 +0530
committeranand <anand.panchdhari@gmail.com>2026-04-06 22:33:59 +0530
commit7e6d5f007c72ad60b1a797e842a1154f58634e11 (patch)
tree2d8783872c7062cff033d57aa8f28ae48acc6f38
parentbfc73d250acfa1bb692e9cfe244a00f183915634 (diff)
loopsmain
-rw-r--r--Cargo.lock7
-rw-r--r--flake.lock27
-rw-r--r--flake.nix34
-rw-r--r--src/main.rs141
4 files changed, 209 insertions, 0 deletions
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 0000000..c69fd41
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 4
+
+[[package]]
+name = "learning"
+version = "0.1.0"
diff --git a/flake.lock b/flake.lock
new file mode 100644
index 0000000..25ce793
--- /dev/null
+++ b/flake.lock
@@ -0,0 +1,27 @@
+{
+ "nodes": {
+ "nixpkgs": {
+ "locked": {
+ "lastModified": 1775036866,
+ "narHash": "sha256-ZojAnPuCdy657PbTq5V0Y+AHKhZAIwSIT2cb8UgAz/U=",
+ "owner": "nixos",
+ "repo": "nixpkgs",
+ "rev": "6201e203d09599479a3b3450ed24fa81537ebc4e",
+ "type": "github"
+ },
+ "original": {
+ "owner": "nixos",
+ "ref": "nixos-unstable",
+ "repo": "nixpkgs",
+ "type": "github"
+ }
+ },
+ "root": {
+ "inputs": {
+ "nixpkgs": "nixpkgs"
+ }
+ }
+ },
+ "root": "root",
+ "version": 7
+}
diff --git a/flake.nix b/flake.nix
new file mode 100644
index 0000000..0d94ad0
--- /dev/null
+++ b/flake.nix
@@ -0,0 +1,34 @@
+{
+ description = "Rust development";
+
+ inputs = {
+ nixpkgs.url = "github:nixos/nixpkgs?ref=nixos-unstable";
+ };
+
+ outputs =
+ { self, nixpkgs }:
+ let
+ pkgs = nixpkgs.legacyPackages.x86_64-linux;
+ in
+ {
+ devShells.x86_64-linux.default = pkgs.mkShell {
+ packages = with pkgs; [
+ gcc
+ bashInteractive
+ rustc
+ clippy
+ cargo
+ rustfmt
+ rust-analyzer
+ ];
+
+ shellHook = ''
+ export SHELL=${pkgs.bashInteractive}/bin/bash
+ export NIX_SHELL_NAME="Rust development"
+ if [[ -n "$PS1" ]]; then
+ PS1="\[\e[38;5;45m\][nix:$NIX_SHELL_NAME]\[\e[0m\] $PS1"
+ fi
+ '';
+ };
+ };
+}
diff --git a/src/main.rs b/src/main.rs
index e7a11a9..ebab809 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,144 @@
fn main() {
println!("Hello, world!");
+ my_funtion();
+ println!("{}", with_parameters(12, 13));
+ let mut string: String = String::from("hello");
+ string.push_str(" world");
+ println!("{string}");
+ let string2 = string;
+ // This no longer works because string2 now owns it and not string
+ // let length = calculate_length(&string);
+ let length = calculate_length(&string2);
+ {
+ let _string3 = string2;
+ }
+ // This no longer works as the value shifted to _string3 and _string3 was dropped
+ // let length = calculate_length(&string2);
+ println!("{length}");
+
+ let mut _x: i32 = 5;
+ let _r: &mut i32 = &mut _x;
+ *_r += 1;
+ // This will give an error
+ // the r was a mutable reference
+ // there can only be one mutable reference
+ // so both _x and _r are mutable references to _x
+ // this gives an error
+
+ // println!("{_x}");
+ println!("{_r}");
+
+ let mut account = BankAccount {
+ owner: "Anand".to_string(),
+ balance: 10000.64,
+ };
+
+ // Immutable borrow to check balance
+ account.check_balance();
+
+ // Mutable borrow to withdraw money
+ account.withdraw(12.3);
+
+ // Immutable borrow to check balance
+ account.check_balance();
+ let hours = 3;
+ println!("{}", hours * HOURS_TO_SECONDS);
+
+ let bs = "bbbbb";
+ println!("{}", bs);
+ let bs = bs.len();
+ println!("{}", bs);
+
+ let age = 11;
+ if age >= 18 {
+ println!("You can drive");
+ } else if age >= 10 {
+ println!("That's a lie");
+ } else {
+ println!("You cannot drive");
+ }
+
+ let condition = true;
+ let inline = if condition {
+ println!("woah");
+ 15
+ } else {
+ 6
+ };
+ println!("{}", inline);
+
+ // Do not do this
+ // loop {
+ // println!("hello world");
+ // }
+
+ let mut counter = 0;
+
+ let result = loop {
+ counter += 1;
+ if counter == 10 {
+ break counter * 2;
+ }
+ };
+ println!("{}", result);
+
+ // Loops can also have labels
+ let mut count = 0;
+ 'counting_up: loop {
+ println!("count = {count}");
+ let mut remaining = 10;
+ loop {
+ println!("remaining = {remaining}");
+ if remaining == 9 {
+ break;
+ }
+ if count == 2 {
+ break 'counting_up;
+ }
+ remaining -= 1;
+ }
+ count += 1;
+ }
+
+ while count <= 10 {
+ println!("count = {count}");
+ count += 1;
+ }
+
+ let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
+ for element in a {
+ println!("{}", element * 2);
+ }
+}
+
+const HOURS_TO_SECONDS: i32 = 60 * 60;
+
+struct BankAccount {
+ owner: String,
+ balance: f64,
+}
+
+impl BankAccount {
+ fn withdraw(&mut self, amt: f64) {
+ println!("Withdrawing {} from {}.", amt, self.owner);
+ self.balance -= amt;
+ }
+
+ fn check_balance(&self) {
+ println!(
+ "Account owned by {} has balance of {}",
+ self.owner, self.balance
+ );
+ }
+}
+
+fn my_funtion() {
+ println!("Hi there");
+}
+
+fn with_parameters(a: i32, b: i32) -> i32 {
+ a + b
+}
+fn calculate_length(s: &String) -> usize {
+ s.len()
}