From 1492157392e0b24f3dfcd5ac054b4bef65987f51 Mon Sep 17 00:00:00 2001 From: Xavier Brinon Date: Sun, 11 Feb 2024 17:20:22 +0000 Subject: [PATCH] Chapter 0103 done --- Projects/hello_cargo/Cargo.toml | 13 ++++ Projects/hello_cargo/src/main.rs | 3 + README.org | 123 +++++++++++++++++++++++++++++-- 3 files changed, 132 insertions(+), 7 deletions(-) create mode 100644 Projects/hello_cargo/Cargo.toml create mode 100644 Projects/hello_cargo/src/main.rs diff --git a/Projects/hello_cargo/Cargo.toml b/Projects/hello_cargo/Cargo.toml new file mode 100644 index 0000000..c7da6d3 --- /dev/null +++ b/Projects/hello_cargo/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "hello_cargo" +version = "0.1.0" +edition = "2021" +authors = ["HaQadosch Berraka "] +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] diff --git a/Projects/hello_cargo/src/main.rs b/Projects/hello_cargo/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/Projects/hello_cargo/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/README.org b/README.org index ec87ea4..32fc27b 100644 --- a/README.org +++ b/README.org @@ -1,3 +1,6 @@ +:PROPERTIES: +:header-args:shell: :results output +:END: #+title: RustBook #+author: HaQadosch Berraka #+date: [2024-02-09 Fri] @@ -8,7 +11,7 @@ * Getting Started ** Chapter 01: Installation -Recommendation is to use ~rustup~ using the command: +Recommendation is to use *rustup* using the command: #+name: install rustup #+begin_src shell curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh @@ -23,18 +26,16 @@ To confirm it's installed: #+RESULTS: confirm rustup installed : rustup 1.26.0 (5af9b9484 2023-04-05) *** Update -To update ~rustup~ and please do regularly +To update *rustup* and please do regularly #+name: update rustup #+begin_src shell :results output rustup update #+end_src #+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 -~rustup~ comes with documentation, just type +*rustup* comes with documentation, just type #+begin_src shell rustup doc #+end_src @@ -67,10 +68,118 @@ To execute this program: #+RESULTS: 0102 call main : Hello, World! -The program wordes, I'm now done. +The program works, I'm now done. *** Note All rust program starts by calling the function ~main~. 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.