{"id":3802,"date":"2025-05-14T07:03:05","date_gmt":"2025-05-14T07:03:05","guid":{"rendered":"https:\/\/mailitics.com\/index.php\/2025\/05\/14\/get-started-with-rust-installation-your-first-cli-tool-a-beginners-guide\/"},"modified":"2025-05-14T07:03:05","modified_gmt":"2025-05-14T07:03:05","slug":"get-started-with-rust-installation-your-first-cli-tool-a-beginners-guide","status":"publish","type":"post","link":"https:\/\/mailitics.com\/index.php\/2025\/05\/14\/get-started-with-rust-installation-your-first-cli-tool-a-beginners-guide\/","title":{"rendered":"Get Started with Rust: Installation and Your First CLI Tool \u2013 A Beginner\u2019s Guide"},"content":{"rendered":"<p>    Get Started with Rust: Installation and Your First CLI Tool \u2013 A Beginner\u2019s Guide<br \/>\n \t<BR><br \/>\n<BR><\/BR><br \/>\n    <!-- no image --><br \/>\n \t<BR><br \/>\n<BR><\/BR><\/p>\n<div>\n<p class=\"wp-block-paragraph\"><mdspan datatext=\"el1747165949018\" class=\"mdspan-comment\">Rust has become<\/mdspan> a popular programming language in recent years as it combines security and high performance and can be used in many applications. It combines the positive characteristics of C and C++ with the modern syntax and simplicity of other programming languages such as Python. In this article, we will take a step-by-step look at the installation of Rust on various operating systems and set up a simple command line interface to understand Rust\u2019s structure and functionality.<\/p>\n<h2 class=\"wp-block-heading\">Installing Rust \u2014 step by step<\/h2>\n<p class=\"wp-block-paragraph\">Regardless of the operating system, it is quite easy to install Rust thanks to the official installer\u00a0<code>rustup<\/code>, which is available for free on the\u00a0<a href=\"https:\/\/www.rust-lang.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Rust<\/a>\u00a0website. This means that the installation only takes a few steps and only differs slightly for the various operating systems.<\/p>\n<h3 class=\"wp-block-heading\">Installing Rust under Windows<\/h3>\n<p class=\"wp-block-paragraph\">In Windows, the installer completely controls the installation, and you can follow the steps below:<\/p>\n<ol class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">Go to the \u201cInstall\u201d subsection on the official Rust website (<a href=\"https:\/\/www.rust-lang.org\/tools\/install\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.rust-lang.org\/tools\/install<\/a>) and download the\u00a0<code>rustup-init.exe<\/code>\u00a0file there. The website recognizes the underlying operating system so that the appropriate suggestions for the system used are made directly.<\/li>\n<li class=\"wp-block-list-item\">As soon as the download is complete, the\u00a0<code>rustup-init.exe<\/code>\u00a0file can be executed. A command line with various installation instructions then opens.<\/li>\n<li class=\"wp-block-list-item\">Press the Enter key to run the standard installation to install Rust. This also includes the following tools:\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">\n<code>rustc<\/code>\u00a0is the compiler, which compiles the code and checks for errors before execution.<\/li>\n<li class=\"wp-block-list-item\">\n<code>cargo<\/code>\u00a0is Rust\u2019s build and package management tool.<\/li>\n<li class=\"wp-block-list-item\">\n<code>rustup<\/code>\u00a0is the version manager.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p class=\"wp-block-paragraph\">After successful installation, Rust should automatically be available in your\u00a0<code>PATH<\/code>. This can be easily checked in PowerShell or CMD using the following commands:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">rustc --version cargo --version<\/code><\/pre>\n<p class=\"wp-block-paragraph\">If \u201crustc\u201d and \u201ccargo\u201d are then displayed in the output with the respective version numbers, then the installation was successful. However, if the command is not found, it may be due to the environment variables. To check these, you can follow the path \u201cThis PC \u2013&gt; Properties \u2013&gt; Advanced system settings \u2013&gt; Environment variables\u201d. Once there, you should make sure that the path to Rust, for example \u201cC:UsersUserName.cargobin\u201d, is present in the\u00a0<code>PATH<\/code>\u00a0variable.<\/p>\n<h3 class=\"wp-block-heading\">Installing Rust under Ubuntu\/Linux<\/h3>\n<p class=\"wp-block-paragraph\">In Linux, Rust can be installed completely via the terminal without having to download anything from the Rust website. To install Rust, the following steps must be carried out:<\/p>\n<ol class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">Open the terminal, for example, with the key combination Ctrl + Alt + T.<\/li>\n<li class=\"wp-block-list-item\">To install Rust, the following command is executed:<\/li>\n<\/ol>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">curl --proto '=https' --tlsv1.2 -sSf https:\/\/sh.rustup.rs | sh<\/code><\/pre>\n<p class=\"wp-block-paragraph\">3. You will then be asked whether the installation should be started. This can be confirmed by entering \u201c1\u201d (default), for example. All required packages are then downloaded, and the environment is set up.<br \/>4. You may have to set the path manually. In this case, you can use this command, for example:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">source $HOME\/.cargo\/env<\/code><\/pre>\n<p class=\"wp-block-paragraph\">After the installation has been completed, you can check whether everything has worked properly. To do this, you can explicitly display the versions of rustc and cargo:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">rustc --version cargo --version<\/code><\/pre>\n<h3 class=\"wp-block-heading\">Installing Rust under macOS<\/h3>\n<p class=\"wp-block-paragraph\">There are several ways to install Rust on macOS. If you have installed Homebrew, you can simply use this to install Rust by executing the following command:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">brew install rustup rustup-init<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Alternatively, you can also install Rust directly using this script:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">curl --proto '=https' --tlsv1.2 -sSf https:\/\/sh.rustup.rs | sh<\/code><\/pre>\n<p class=\"wp-block-paragraph\">During the installation, you will be asked whether you want to run the standard installation. You can simply confirm this by pressing the Enter key. Regardless of the variant selected, you can then check the installation by displaying the version of Rust to ensure that everything has worked:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">rustc --version \ncargo --version<\/code><\/pre>\n<h2 class=\"wp-block-heading\">Creating a Rust project with cargo<\/h2>\n<p class=\"wp-block-paragraph\">During the installation of Rust, you have probably already come across the\u00a0<code>cargo<\/code>\u00a0program. This is the official package manager and build system of Rust and is comparable to\u00a0<code>pip<\/code>\u00a0in Python.\u00a0<code>cargo<\/code>\u00a0performs the following tasks, among others:<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">Initialization of a project<\/li>\n<li class=\"wp-block-list-item\">Management of dependencies<\/li>\n<li class=\"wp-block-list-item\">Compiling the code<\/li>\n<li class=\"wp-block-list-item\">Execution of tests<\/li>\n<li class=\"wp-block-list-item\">Optimization of builds<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\">This allows you to manage complete projects in Rust without having to deal with complicated build scripts. It also helps you to set up new projects quickly and easily, which can then be filled with life.<\/p>\n<p class=\"wp-block-paragraph\">For our example, we will create a new project. To do this, we go to the terminal and navigate to a folder in which we want to save it. We then execute the following command to create a new Rust project:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">cargo new json_viewer --bin<\/code><\/pre>\n<p class=\"wp-block-paragraph\">We call this project\u00a0<code>json_viewer<\/code>\u00a0because we are building a CLI tool that can be used to open and process\u00a0<a href=\"https:\/\/databasecamp.de\/en\/data\/json-en\" target=\"_blank\" rel=\"noreferrer noopener\">JSON<\/a>\u00a0files. The\u00a0<code>--bin<\/code>\u00a0option indicates that we want to create an executable program and not a library. You should now be able to see the following folder structure in your directory after executing the command:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">json_viewer\/ \n\u251c\u2500\u2500 Cargo.toml # Project configuration \n\u2514\u2500\u2500 src \n     \u2514\u2500\u2500 main.rs # File for Rust Code<\/code><\/pre>\n<p class=\"wp-block-paragraph\">Every new project has this structure.\u00a0<code>Cargo.toml<\/code>\u00a0contains all the dependencies and metadata of your project, such as the name, the libraries used, or the version. The\u00a0<code>src\/main.rs<\/code>, on the other hand, later contains the actual Rust code, which then defines the steps that are executed when the program is started.<\/p>\n<p class=\"wp-block-paragraph\">First, we can define a simple function here that generates an output in the terminal:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-rust\">fn main() { \n     println!(\"Hello, Rust CLI-Tool!\"); \n}<\/code><\/pre>\n<p class=\"wp-block-paragraph\">The program can be easily called up from the terminal using\u00a0<code>cargo<\/code>:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">cargo run<\/code><\/pre>\n<p class=\"wp-block-paragraph\">For this call to work, it must be ensured that you are in the main directory of the project, i.e. where the\u00a0<code>Cargo.toml<\/code>\u00a0file is stored. If everything has been set up and executed correctly, you will receive this output:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">Hello, Rust CLI-Tool!<\/code><\/pre>\n<p class=\"wp-block-paragraph\">With these few steps, you have just created your first successful Rust project, which we can build on in the next section.<\/p>\n<h2 class=\"wp-block-heading\">Building a CLI tool: Simple JSON parser<\/h2>\n<p class=\"wp-block-paragraph\">Now we start to fill the project with life and create a program that can read JSON files and output their content in the terminal in a structured way.<\/p>\n<p class=\"wp-block-paragraph\">The first step is to define the dependencies, i.e., the libraries that we will use in the course of the project. These are stored in the\u00a0<code>Cargo.toml\u00a0<\/code>file. In Rust, the so-called crates are comparable to libraries or modules that offer certain predefined functionalities. For example, they can consist of reusable code written by other developers.<\/p>\n<p class=\"wp-block-paragraph\">We need the following crates for our project:<\/p>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">\n<code>serde<\/code>\u00a0enables the serialization and deserialization of data formats such as\u00a0<a href=\"https:\/\/databasecamp.de\/en\/data\/json-en\">JSON<\/a>\u00a0or\u00a0<a href=\"https:\/\/databasecamp.de\/en\/data\/yaml-file\">YAML<\/a>.<\/li>\n<li class=\"wp-block-list-item\">\n<code>serde_json<\/code>, on the other hand, is an extension that was developed specifically for working with JSON files.<\/li>\n<\/ul>\n<p class=\"wp-block-paragraph\">For your project to access these crates, they must be stored in the\u00a0<code>Cargo.toml<\/code>\u00a0file. This looks like this immediately after creating the project:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">[package] \nname = \"json_viewer\" \nversion = \"0.1.0\" \nedition = \"2021\" \n\n[dependencies]<\/code><\/pre>\n<p class=\"wp-block-paragraph\">We can now add the required crates in the\u00a0<code>[dependencies]<\/code>\u00a0section. Here we also define the version to be used:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">[dependencies] \nserde = \"1.0\" \nserde_json = \"1.0\"<\/code><\/pre>\n<p class=\"wp-block-paragraph\">To ensure that the added dependencies are available in the project, they must first be downloaded and built. To do this, the following terminal command can be executed in the main directory:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">cargo build<\/code><\/pre>\n<p class=\"wp-block-paragraph\">During execution,\u00a0<code>cargo<\/code>\u00a0searches the central Rust repository crates.io for the dependencies and the specified versions to download them. These crates are then compiled together with the code and cached so that they do not have to be downloaded again for the next build.<\/p>\n<p class=\"wp-block-paragraph\">If these steps have worked, we are now ready to write the actual Rust code that opens and processes the JSON file. To do this, you can open the\u00a0<code>src\/main.rs<\/code>\u00a0file and replace the existing content with this code:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-rust\">use std::fs;\nuse serde_json::Value;\nuse std::env;\n\nfn main() {\n    \/\/ Check arguments\n    let args: Vec&lt;String&gt; = env::args().collect();\n    if args.len() &lt; 2 {\n        println!(\u201cPlease specify the path to your file.\u201d);\n        return;\n    }\n\n    \/\/ Read in File\n    let file_path = &amp;args[1];\n    let file_content = fs::read_to_string(file_path)\n        .expect(\u201cFile could not be read.\u201d);\n\n    \/\/ Parse JSON\n    let json_data: Value = serde_json::from_str(&amp;file_content)\n        .expect(\u201cInvalid JSON format.\u201d);\n\n    \/\/ Print JSON\n    println!(\" JSON-content:n{}\u201d, json_data);\n}<\/code><\/pre>\n<p class=\"wp-block-paragraph\">The code follows these steps:<\/p>\n<ol class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">Check arguments:\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">We read the arguments from the command line via\u00a0<code>env::args()<\/code>.<\/li>\n<li class=\"wp-block-list-item\">The user must specify the path to the JSON file at startup.<\/li>\n<\/ul>\n<\/li>\n<li class=\"wp-block-list-item\">Read the file:\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">With the help of\u00a0<code>fs::read_to_string()<\/code>, the content of the file is read into a string.<\/li>\n<\/ul>\n<\/li>\n<li class=\"wp-block-list-item\">Parse JSON:\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">The crate\u00a0<code>serde_json<\/code>\u00a0converts the string into a Rust object with the type Value.<\/li>\n<\/ul>\n<\/li>\n<li class=\"wp-block-list-item\">Format output:\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">The content is output legibly in the console.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n<p class=\"wp-block-paragraph\">To test the tool, you can, for example, create a test file in the project directory under the name\u00a0<code>examples.json<\/code>:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-json\">{\n  \"name\": \"Alice\",\n  \"age\": 30,\n  \"skills\": [\"Rust\", \"Python\", \"Machine Learning\"]\n}<\/code><\/pre>\n<p class=\"wp-block-paragraph\">The program is then executed using\u00a0<code>cargo run<\/code>\u00a0and the path to the JSON file is also defined:<\/p>\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-bash\">cargo run .\/example.json<\/code><\/pre>\n<p class=\"wp-block-paragraph\">This brings us to the end of our first project in Rust and we have successfully built a simple CLI tool that can read JSON files and output them to the command line.<\/p>\n<h2 class=\"wp-block-heading\">This is what you should take with you<\/h2>\n<ul class=\"wp-block-list\">\n<li class=\"wp-block-list-item\">Installing Rust is quick and easy in many operating systems. The other components that are required are already installed.<\/li>\n<li class=\"wp-block-list-item\">With the help of\u00a0<code>cargo<\/code>, an empty project can be created directly, which contains the necessary files and in which you can start writing Rust code<\/li>\n<li class=\"wp-block-list-item\">Before you start <a href=\"https:\/\/towardsdatascience.com\/tag\/programming\/\" title=\"Programming\">Programming<\/a>, you should enter the dependencies and download them using the build command.<\/li>\n<li class=\"wp-block-list-item\">Now that everything is set up, you can start with the actual programming.\n<\/li>\n<\/ul>\n<p>The post <a href=\"https:\/\/towardsdatascience.com\/get-started-with-rust-installation-your-first-cli-tool-a-beginners-guide\/\">Get Started with Rust: Installation and Your First CLI Tool \u2013 A Beginner\u2019s Guide<\/a> appeared first on <a href=\"https:\/\/towardsdatascience.com\/\">Towards Data Science<\/a>.<\/p>\n<\/div>\n<p> \t<BR><br \/>\n <BR><\/BR><br \/>\n    Niklas Lang<br \/>\n \t<BR><br \/>\n<BR><\/BR><br \/>\n<a href=\"https:\/\/towardsdatascience.com\/get-started-with-rust-installation-your-first-cli-tool-a-beginners-guide\/\">Go to original source<\/a><br \/>\n \t<BR><br \/>\n <BR><\/BR><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Get Started with Rust: Installation and Your First CLI Tool \u2013 A Beginner\u2019s Guide Rust has become a popular programming language in recent years as it combines security and high performance and can be used in many applications. It combines the positive characteristics of C and C++ with the modern syntax and simplicity of other [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62,2667,160,1870,2668,302],"tags":[2669,1873,430],"class_list":["post-3802","post","type-post","status-publish","format-standard","hentry","category-aimldsaimlds","category-json","category-programming","category-rust","category-rust-cargo-package","category-software-engineering","tag-installation","tag-rust","tag-step"],"_links":{"self":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts\/3802"}],"collection":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/comments?post=3802"}],"version-history":[{"count":0,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/posts\/3802\/revisions"}],"wp:attachment":[{"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/media?parent=3802"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/categories?post=3802"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mailitics.com\/index.php\/wp-json\/wp\/v2\/tags?post=3802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}