CroftSoft / Library / Tutorials

Rust Project Setup

2024 Jan 21 Sun

David Wallace Croft


Contents

Project Setup

  1. Install the Rust programming language on your development computer rustup update
    • After installing or updating, check the version of the Rust compiler "rustc"
    rustc --version
    • The Rust installation includes the command-line interface (CLI) utility "cargo"
    cargo --version
  2. Install Visual Studio Code (VS Code)
  3. Install the following VS Code extensions
  4. Install the git version control client
  5. Use cargo to make the project directory
    • The project directory will be a child of your current working directory
    • Use a lowercase-hyphenated value such as "project-name"
    cargo new project-name
  6. Test your setup
    • You should see "Hello, world!" as the output
    cd project-name/ cargo run
  7. Open VS Code from the root of the project directory code .
  8. Optional: If want to license your code to others, make a LICENSE.txt file
    • The following example uses the open source MIT license
    • If you use this example, replace the copyright-year and copyright-holder-name
    MIT License Copyright (c) [copright-year] [copyright-holder-name] Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  9. Optional: Make a README.md file in your project root directory
    • Use the Markdown syntax documented at https://www.markdownguide.org/
    • If you use the following example, replace the following placeholder values:
      • "Project Name", "Project Description", "project-name", "YYYY-MM-DD"
    # Project Name - Project Description ## Usage - cd project-name/ - cargo run ## History - Initial release: YYYY-MM-DD
  10. Optional: Make a rustfmt.toml in your project root directory
    • This will configure how VS Code formats your Rust source code
    • You can also format your code from the command-line using "cargo fmt"
    • To customize the following example, see https://rust-lang.github.io/rustfmt/
    # https://rust-lang.github.io/rustfmt/ array_width = 1 # condense_wildcard_suffixes = true fn_params_layout = "Vertical" # group_imports = "StdExternalCrate" hard_tabs = false # imports_layout = "HorizontalVertical" match_block_trailing_comma = true # https://www.w3.org/WAI/tutorials/page-structure/styling/#line-length max_width = 80 merge_derives = true newline_style = "Unix" # normalize_comments = true remove_nested_parens = true single_line_if_else_max_width = 0 single_line_let_else_max_width = 0 struct_lit_width = 0 struct_variant_width = 0 tab_spaces = 2 use_field_init_shorthand = true use_try_shorthand = true # version = "Two" # wrap_comments = true
  11. Optional: Replace the package section of your Cargo manifest file Cargo.toml
    • Update the required "name" property to match the name of your project directory
    • Unremark and update the remarked out properties as required
    [package] # authors = ["First Middle Last <email.username@domain-name.com>"] # Use the categories from https://crates.io/categories # categories = ["category-name"] # description = "project description" edition = "2021" # homepage = "https://www.domain-name.com/" # https://doc.rust-lang.org/cargo/reference/manifest.html#the-keywords-field # keywords = ["keyword"] # Unremark and update this property if your project is open source # license = "MIT" name = "project-name" # Unremark and change this value to true if you publish to crates.io # publish = false # Unremark this property if you created a README.md file # readme = "README.md" # Unremark this line if you will be using a remote code repository # repository = "https://github.com/github-username/project-name" version = "0.1.0"
  12. Commit your code changes
    • You can commit your code from within VS Code
    • Or you can commit from the command-line
    git add . git commit -m "Rust project setup"
  13. Create the remote repository for your source code
    • If you are using GitHub as your remote repository service provider
      • VS Code might be able to create the repository for you the first time you push
    • Or create a remote repository using your browser-based user interface (UI)
  14. Make your first push to your remote code repository
    • This assumes you have not already pushed within VS Code to create the repository
    • Replace the placeholder value "url" with the URL of your remote repository
    • You can find the URL for your remote repository in its browser-based UI
    git remote add origin [url] git push -u origin main

Links

In recommended reading order


© 2022 - 2024 CroftSoft Inc

 
 
 
CroftSoft
 
 
About
Library
- Books
- Code
- Courses
- Links
- Media
- Software
- Tutorials
People
Portfolio
Update
 
 
Google
CroftSoft Web