Rust Basics(how to install Rust, Cargo)

Yechan Jeon·2022년 1월 14일
0

Rust dev

목록 보기
1/4
post-thumbnail

Start Rust


Make a boilerplate

In your terminal , Type this command line cargo new <project name> and you can check boilerplate in your project file.

  1. 'Cargo.toml' is similar to package.json in Node.js
  2. In src directory there is main.rs where you will write a code.

Hello world in Rust

  • src/main.rs
fn main() {
    println!("Hello, world!");
}
  • Write Hello world
    Rust shares low-level feature , so to run Rust code, compile it first.
  1. rustc <filename> if you want to compile it as binary code.
  2. Then you can find a exe file generated by compiling code
  3. In Terminal, execute it ./maiu.exe
  • Anatomy of Hello world
    Let's get into a function more detail.
  1. 'fn' is the way to declare function in Rust
  2. 'main' is special funciton , This will be always first code runs in executable rust program
  3. 'println' is to print text to the screen. If you add exclamation mark 'println!' , It is called as 'Rust marcos' (more detail later)
  4. It always needs ';' semicolon to end the line

Rust is an ahead-of-time compiled language.
Languages such as Ruby, Python, or Javascript is needed to give its file to execute them.
But in compiled language, you only need to compile and can give the executable program to someone else.

Cargo

Cargo is build system and package manager. (sounds like npm in node?)
You can check if cargo works or not with cargo --version in your terminal.

  1. create a project with cargo
    cargo new <project name>

  2. src directory
    Cargo expects your source files to live inside in src directory

  3. Cargo.toml
    Where cargo treats dependencies

  4. building and running a cargo project
    cargo build
    It generates executable program in 'target/debug' path as your project name.

  5. easy way to compile and run altogether
    cargo run
    If you had modified any code, Cargo would rebuilt the project before running it by compiling again.

  6. Speed up your development
    cargo check
    It doesn't product anything, just check. So it can be able to make your development speed faster.

Recap

  • We can build a project using cargo build
  • We can build and run a project in one step using cargo run.
  • We can build a project without producing a binary to check for errors using cargo check.
  • Instead of saving the result of the build in the same directory as our code, Cargo stores it in the target/debug directory.
profile
방황했습니다. 다시 웹 개발 하려고요!

0개의 댓글