Initialize your Rust Project
In this step, we will initialize a basic rust project, where we can start building our simple Rust state machine.
cargo init
-
Create a directory where you want your project to live, and navigate to that folder. We will be using a folder named
rust-state-machine
.mkdir rust-state-machine cd rust-state-machine
-
In that folder, initialize your rust project using
cargo init
:cargo init
This will scaffold a basic Rust executable which we can use to start building.
-
You can verify that your new project is working as expected by running:
cargo run
You should see "Hello, World!" appear at the end of the compilation:
➜ rust-state-machine git:(master) ✗ cargo run Compiling rust-state-machine v0.1.0 (/Users/shawntabrizi/Documents/GitHub/rust-state-machine) Finished dev [unoptimized + debuginfo] target(s) in 2.19s Running `target/debug/rust-state-machine` Hello, world!
If we look at what has been generated, in that folder, you will see the following:
- src/main.rs - This is the entry point to your program. We will be building everything for this project in the
src
folder. - Cargo.toml - This is a configuration file for your Rust project. Quite similar to a
package.json
that you would see in a Node.JS project. We will modify this in the future when we import crates to use in our project, but We can leave this alone for now. - Cargo.lock - This is an autogenerated lock file based on your
cargo.toml
and the compilation. This usually defines the very specific versions of each crate being imported, and should not be manually edited. target/*
- You might also see a target folder if you didcargo run
. This is a folder where all the build artifacts are placed during compilation. We do not commit this folder into our git history.
All of this should be pretty familiar to you if you have already had some minimal experience with Rust. If any of this is new, I would suggest you first walk through the Rust Book and Rust by Example, as this is already an indication that this guide might not be targeted at your level of knowledge.
[package]
name = "rust-state-machine"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
fn main() { println!("Hello, world!"); }
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 00000000..400485bd
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+**/target/
+.DS_Store
diff --git a/Cargo.lock b/Cargo.lock
new file mode 100644
index 00000000..75fe297f
--- /dev/null
+++ b/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "rust-state-machine"
+version = "0.1.0"
diff --git a/Cargo.toml b/Cargo.toml
new file mode 100644
index 00000000..c7fe36cd
--- /dev/null
+++ b/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "rust-state-machine"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/src/main.rs b/src/main.rs
new file mode 100644
index 00000000..e7a11a96
--- /dev/null
+++ b/src/main.rs
@@ -0,0 +1,3 @@
+fn main() {
+ println!("Hello, world!");
+}