Chapter 0103 done
This commit is contained in:
parent
27287dead0
commit
1492157392
13
Projects/hello_cargo/Cargo.toml
Normal file
13
Projects/hello_cargo/Cargo.toml
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
[package]
|
||||||
|
name = "hello_cargo"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
authors = ["HaQadosch Berraka <haqadosch+rust@pm.me>"]
|
||||||
|
description = "displays hello world"
|
||||||
|
documentation = "./docs/ref.org"
|
||||||
|
readme = "./docs/readme.org"
|
||||||
|
|
||||||
|
|
||||||
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
[dependencies]
|
3
Projects/hello_cargo/src/main.rs
Normal file
3
Projects/hello_cargo/src/main.rs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
fn main() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
}
|
121
README.org
121
README.org
@ -1,3 +1,6 @@
|
|||||||
|
:PROPERTIES:
|
||||||
|
:header-args:shell: :results output
|
||||||
|
:END:
|
||||||
#+title: RustBook
|
#+title: RustBook
|
||||||
#+author: HaQadosch Berraka
|
#+author: HaQadosch Berraka
|
||||||
#+date: [2024-02-09 Fri]
|
#+date: [2024-02-09 Fri]
|
||||||
@ -8,7 +11,7 @@
|
|||||||
* Getting Started
|
* Getting Started
|
||||||
|
|
||||||
** Chapter 01: Installation
|
** Chapter 01: Installation
|
||||||
Recommendation is to use ~rustup~ using the command:
|
Recommendation is to use *rustup* using the command:
|
||||||
#+name: install rustup
|
#+name: install rustup
|
||||||
#+begin_src shell
|
#+begin_src shell
|
||||||
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
|
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
|
||||||
@ -23,18 +26,16 @@ To confirm it's installed:
|
|||||||
#+RESULTS: confirm rustup installed
|
#+RESULTS: confirm rustup installed
|
||||||
: rustup 1.26.0 (5af9b9484 2023-04-05)
|
: rustup 1.26.0 (5af9b9484 2023-04-05)
|
||||||
*** Update
|
*** Update
|
||||||
To update ~rustup~ and please do regularly
|
To update *rustup* and please do regularly
|
||||||
#+name: update rustup
|
#+name: update rustup
|
||||||
#+begin_src shell :results output
|
#+begin_src shell :results output
|
||||||
rustup update
|
rustup update
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
#+RESULTS: update rustup
|
#+RESULTS: update rustup
|
||||||
:
|
|
||||||
: stable-x86_64-unknown-linux-gnu unchanged - rustc 1.76.0 (07dca489a 2024-02-04)
|
: stable-x86_64-unknown-linux-gnu unchanged - rustc 1.76.0 (07dca489a 2024-02-04)
|
||||||
:
|
|
||||||
*** Documentation
|
*** Documentation
|
||||||
~rustup~ comes with documentation, just type
|
*rustup* comes with documentation, just type
|
||||||
#+begin_src shell
|
#+begin_src shell
|
||||||
rustup doc
|
rustup doc
|
||||||
#+end_src
|
#+end_src
|
||||||
@ -67,10 +68,118 @@ To execute this program:
|
|||||||
#+RESULTS: 0102 call main
|
#+RESULTS: 0102 call main
|
||||||
: Hello, World!
|
: Hello, World!
|
||||||
|
|
||||||
The program wordes, I'm now done.
|
The program works, I'm now done.
|
||||||
|
|
||||||
*** Note
|
*** Note
|
||||||
All rust program starts by calling the function ~main~.
|
All rust program starts by calling the function ~main~.
|
||||||
Functions with ~!~ appended to their name, like ~println!~, are macros.
|
Functions with ~!~ appended to their name, like ~println!~, are macros.
|
||||||
|
|
||||||
|
** Chapter 03: Hello, Cargo!
|
||||||
|
:PROPERTIES:
|
||||||
|
:header-args:shell: :dir ./Projects/hello_cargo :results output
|
||||||
|
:END:
|
||||||
|
|
||||||
|
Make sure *Cargo* is installed
|
||||||
|
#+name: cargo version
|
||||||
|
#+begin_src shell
|
||||||
|
cargo --version
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
#+RESULTS: cargo version
|
||||||
|
: cargo 1.76.0 (c84b36747 2024-01-18)
|
||||||
|
|
||||||
|
*** New project with *Cargo*
|
||||||
|
#+name: new cargo
|
||||||
|
#+begin_src shell
|
||||||
|
cargo new Projects/hello_cargo
|
||||||
|
tree Projects/hello_cargo
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
#+RESULTS: new cargo
|
||||||
|
: Projects/hello_cargo
|
||||||
|
: ├── Cargo.toml
|
||||||
|
: └── src
|
||||||
|
: └── main.rs
|
||||||
|
:
|
||||||
|
: 2 directories, 2 files
|
||||||
|
|
||||||
|
*Cargo* creates:
|
||||||
|
- a directory with the project name given,
|
||||||
|
- and a project with the same name
|
||||||
|
|
||||||
|
The *Cargo* configuration file =Cargo.toml= in the root folder and
|
||||||
|
the projcet code in the =src/= directiory is what makes it a *Rust* project.
|
||||||
|
|
||||||
|
*** Building with Cargo
|
||||||
|
From the root folder, run
|
||||||
|
#+name: buiild cargo
|
||||||
|
#+begin_src shell
|
||||||
|
cargo build
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
#+RESULTS: buiild cargo
|
||||||
|
|
||||||
|
This will create a =Cargo.lock= file in the root directory, and
|
||||||
|
an executable in =src/target/debug/hello_cargo=.
|
||||||
|
Which means you can display the text "hello, World!" by calling directly the file
|
||||||
|
#+name: direct call
|
||||||
|
#+begin_src shell
|
||||||
|
./target/debug/hello_cargo
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
#+RESULTS: direct call
|
||||||
|
: Hello, world!
|
||||||
|
|
||||||
|
*** Running a cargo
|
||||||
|
You can both build and run the program with
|
||||||
|
#+name: run hello_cargo
|
||||||
|
#+begin_src shell
|
||||||
|
cargo run
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
#+RESULTS: run hello_cargo
|
||||||
|
: Hello, world!
|
||||||
|
|
||||||
|
*** Checking a cargo
|
||||||
|
*Cargo* can check if the program can compile. It doesn't execute it.
|
||||||
|
#+name: check hello_cargo
|
||||||
|
#+begin_src shell
|
||||||
|
cargo check
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
#+RESULTS: check hello_cargo
|
||||||
|
: Finished dev [unoptimized + debuginfo] target(s) in 0.01s
|
||||||
|
: [ Babel evaluation exited with code 0 ]
|
||||||
|
|
||||||
|
*** Releasing a cargo
|
||||||
|
To build a cargo with an optimised build, run
|
||||||
|
|
||||||
|
#+name: release hello_cargo
|
||||||
|
#+begin_src shell
|
||||||
|
cargo build --release
|
||||||
|
tree
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
#+RESULTS: release hello_cargo
|
||||||
|
#+begin_example
|
||||||
|
.
|
||||||
|
├── Cargo.lock
|
||||||
|
├── Cargo.toml
|
||||||
|
├── src
|
||||||
|
│ └── main.rs
|
||||||
|
└── target
|
||||||
|
├── CACHEDIR.TAG
|
||||||
|
└── release
|
||||||
|
├── build
|
||||||
|
├── deps
|
||||||
|
│ ├── hello_cargo-ac6d8d77e18d4d6b
|
||||||
|
│ └── hello_cargo-ac6d8d77e18d4d6b.d
|
||||||
|
├── examples
|
||||||
|
├── hello_cargo
|
||||||
|
├── hello_cargo.d
|
||||||
|
└── incremental
|
||||||
|
|
||||||
|
8 directories, 8 files
|
||||||
|
#+end_example
|
||||||
|
|
||||||
|
The build results are in the folder =target/release/= now.
|
||||||
|
Loading…
Reference in New Issue
Block a user