Flatten nested Rust projects into monorepo

This commit is contained in:
2025-07-01 10:51:25 +03:00
commit 1170ebe04d
29 changed files with 557 additions and 0 deletions

11
.gitignore vendored Normal file
View File

@@ -0,0 +1,11 @@
**/target/
**/*.o
**/*.d
**/*.exe
**/*.log
**/*.rlib
**/*.dylib
**/*.so
**/.DS_Store
**/.vscode/
**/.idea/

11
Cargo.toml Normal file
View File

@@ -0,0 +1,11 @@
[workspace]
members = [
"branches",
"data_types",
"functions",
"guessing_game",
"hello_cargo",
"loops",
"ownership",
"variables",
]

7
branches/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "branches"
version = "0.1.0"

6
branches/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "branches"
version = "0.1.0"
edition = "2024"
[dependencies]

20
branches/src/main.rs Normal file
View File

@@ -0,0 +1,20 @@
fn main() {
let condition = true;
let result = if condition { 5 } else { 6 };
// let result = if condition { 5 } else { 'six' };
// will throw an error, should be the same type
println!("The result is {result}");
let number = 6;
if number % 4 == 0 {
println!("number is divisible by 4");
} else if number % 3 == 0 {
println!("number is divisible by 3");
} else if number % 2 == 0 {
println!("number is divisible by 2");
} else {
println!("number is not divisible by 4, 3, or 2");
}
}

75
cleanup.sh Executable file
View File

@@ -0,0 +1,75 @@
#!/bin/bash
# This script assumes you're in the monorepo root directory.
# It will:
# - Remove nested .git folders
# - Remove subproject .gitignore files (if present)
# - Optionally create or update the workspace Cargo.toml
# - Add the projects to your root Git repo
set -e
echo "🔧 Flattening nested Rust projects into a monorepo..."
# Gather all Rust subprojects
projects=()
for dir in */Cargo.toml; do
project=$(dirname "$dir")
projects+=("$project")
# Remove nested Git repo
if [ -d "$project/.git" ]; then
echo "❌ Removing nested Git repo in $project"
rm -rf "$project/.git"
fi
# Optionally remove local .gitignore (you can comment this out if needed)
if [ -f "$project/.gitignore" ]; then
echo "❌ Removing $project/.gitignore"
rm -f "$project/.gitignore"
fi
done
# Create .gitignore in root if missing
if [ ! -f .gitignore ]; then
echo "📄 Creating root .gitignore"
cat > .gitignore <<EOF
**/target/
**/*.o
**/*.d
**/*.exe
**/*.log
**/*.rlib
**/*.dylib
**/*.so
**/.DS_Store
**/.vscode/
**/.idea/
EOF
fi
# Create root Cargo.toml workspace
if [ ! -f Cargo.toml ]; then
echo "📄 Creating root Cargo.toml"
echo "[workspace]" > Cargo.toml
echo "members = [" >> Cargo.toml
for project in "${projects[@]}"; do
echo " \"$project\"," >> Cargo.toml
done
echo "]" >> Cargo.toml
else
echo "⚠️ Cargo.toml already exists at root. You may need to manually add workspace members."
fi
# Init root Git repo if not already
if [ ! -d .git ]; then
echo "🔁 Initializing new root Git repo"
git init
fi
# Add and commit
git add .
git commit -m "Flatten nested Rust projects into monorepo"
echo "✅ Done! Monorepo is clean and ready."

7
data_types/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "data_types"
version = "0.1.0"

6
data_types/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "data_types"
version = "0.1.0"
edition = "2024"
[dependencies]

35
data_types/src/main.rs Normal file
View File

@@ -0,0 +1,35 @@
use std::io;
fn main() {
let tup: (u32, f64, i8) = (500, 420.69, 1); // different types
let fiveh = tup.0;
let fourtw = tup.1;
let one = tup.2;
let (x, y, z) = tup;
let nums: [i32; 5] = [1, 2, 3, 4, 5]; // only one data type
let a = [3; 5]; // same as
// let a = [3, 3, 3, 3, 3];
let first = nums[0];
let second = nums[1];
let a = [1, 2, 3, 4, 5];
println!("Type an index");
let mut index = String::new();
io::stdin()
.read_line(&mut index)
.expect("Failed to read line");
let index: usize = index
.trim()
.parse()
.expect("Index wasn't a valid numver");
let element = a[index];
println!("The element for the index {index} is: {element}");
}

7
functions/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "functions"
version = "0.1.0"

6
functions/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "functions"
version = "0.1.0"
edition = "2024"
[dependencies]

32
functions/src/main.rs Normal file
View File

@@ -0,0 +1,32 @@
fn main() {
another(10);
measure(10, 'm');
let a = numba();
println!("Value of a: {a}");
let x = {
let y = 3;
y + 2 // returns a Value
// but:
// let x = {
// let y = 3;
// y + 2;
// };
// just a statement, doesnt return any value
}; // This is an expression
println!("Value of x: {x}");
}
fn another(x: u32) {
println!("{x}");
}
fn measure(height: i32, unit: char) {
println!("Measuring... {height}{unit}.");
}
fn numba() -> i32 {
69
}

133
guessing_game/Cargo.lock generated Normal file
View File

@@ -0,0 +1,133 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "cfg-if"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9555578bc9e57714c812a1f84e4fc5b4d21fcb063490c624de019f7464c91268"
[[package]]
name = "getrandom"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "guessing_game"
version = "0.1.0"
dependencies = [
"rand",
]
[[package]]
name = "libc"
version = "0.2.174"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1171693293099992e19cddea4e8b849964e9846f4acee11b3948bcc337be8776"
[[package]]
name = "ppv-lite86"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
dependencies = [
"zerocopy",
]
[[package]]
name = "proc-macro2"
version = "1.0.95"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
]
[[package]]
name = "rand_core"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]]
name = "syn"
version = "2.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17b6f705963418cdb9927482fa304bc562ece2fdd4f616084c50b7023b435a40"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "wasi"
version = "0.11.1+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
[[package]]
name = "zerocopy"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

7
guessing_game/Cargo.toml Normal file
View File

@@ -0,0 +1,7 @@
[package]
name = "guessing_game"
version = "0.1.0"
edition = "2024"
[dependencies]
rand = "0.8.5"

50
guessing_game/src/main.rs Normal file
View File

@@ -0,0 +1,50 @@
// use std::cmp::Ordering;
use std::io;
use rand::Rng;
fn main() {
println!("Guess a numba");
let secret_number = rand::thread_rng().gen_range(1..=100);
// println!("The secret numba is: {secret_number}");
let mut number_of_guesses = 0;
loop {
println!("Do your input");
let mut guess = String::new();
io::stdin().read_line(&mut guess).expect("Nothing here");
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
println!("Your guess: {guess}");
if guess > secret_number {
println!("Secret number is smaller!");
} else if guess < secret_number {
println!("Secret number is bigger!");
} else {
println!("You got me! Secret number is: {secret_number}");
break;
}
number_of_guesses += 1;
// match guess.cmp(&secret_number) {
// Ordering::Less => println!("Bigger!"),
// Ordering::Greater => println!("Smaller!"),
// Ordering::Equal => {
// println!("Got me :(");
// break;
// }
// }
}
println!("\nIt took you {number_of_guesses} times to guess correctly!");
}

7
hello_cargo/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "hello_cargo"
version = "0.1.0"

6
hello_cargo/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2024"
[dependencies]

3
hello_cargo/src/main.rs Normal file
View File

@@ -0,0 +1,3 @@
fn main() {
println!("Hello, world!");
}

BIN
hello_world/main Executable file

Binary file not shown.

3
hello_world/main.rs Normal file
View File

@@ -0,0 +1,3 @@
fn main() {
println!("Hello World!");
}

7
loops/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "loops"
version = "0.1.0"

6
loops/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "loops"
version = "0.1.0"
edition = "2024"
[dependencies]

65
loops/src/main.rs Normal file
View File

@@ -0,0 +1,65 @@
fn main() {
let mut counter = 0;
let result = loop {
counter += 1;
if counter == 10 {
break counter * 6;
}
};
println!("Result is: {result}");
let mut count = 0;
'inner_loop: loop {
println!("Counter: {count}");
let mut remaining = 10;
loop {
println!("Remain: {remaining}");
if remaining == 9 {
break;
}
if count == 2 {
break 'inner_loop;
}
remaining -= 1;
}
count += 1;
}
println!("End of counter: {count}");
let mut number = 3;
while number != 0 {
println!("{number}!");
number -= 1;
}
println!("LIFTOFF!!!");
let a = [10, 20, 30, 40, 50];
let mut index = 0;
while index < 5 {
println!("the value is: {}", a[index]);
index += 1;
}
for element in a {
println!("the value is: {element}");
} // this is better and safer usecase for listing elements of an array
for number in (1..4).rev() {
println!("{number}!");
}
println!("LIFTOFF!!!");
}

7
ownership/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "ownership"
version = "0.1.0"

6
ownership/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "ownership"
version = "0.1.0"
edition = "2021"
[dependencies]

5
ownership/src/main.rs Normal file
View File

@@ -0,0 +1,5 @@
fn main() {
let mut s = String::from("Hello");
s.push_str(", World!");
println!("{s}");
}

7
variables/Cargo.lock generated Normal file
View File

@@ -0,0 +1,7 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "variables"
version = "0.1.0"

6
variables/Cargo.toml Normal file
View File

@@ -0,0 +1,6 @@
[package]
name = "variables"
version = "0.1.0"
edition = "2024"
[dependencies]

16
variables/src/main.rs Normal file
View File

@@ -0,0 +1,16 @@
fn main() {
let x = 10;
let x = x * 5;
{
let x = x / 2;
println!("x in the inner scope: {x}");
}
println!("x in the outer scope: {x}");
// let mut space = " ";
// space = space.len();
// println!("spaces: {space}");
}