0% found this document useful (0 votes)
492 views7 pages

Rust in Action v1.0

Uploaded by

Anjum Nazir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
492 views7 pages

Rust in Action v1.0

Uploaded by

Anjum Nazir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Rust in Action

Chapter – 01

Rust installation
$ curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf -o rust_setup.sh

$ curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh

$ ./rust_setup.sh

Rust Update
$ rustup update

Rust Uninstall
$ rustup self uninstall

In Rust compilation and execution are separate processes

Cargo:
 Cargo is Rust’s (1) build system and (2) package manager.
 Cargo:
o Builds your code
o Download and build the libraries /dependencies.
$ cargo --version

### Creating a Project with Cargo ###


$ cargo new hello_cargo

$ cd hello_cargo

Cargo has generated two files and one directory for us:
 Cargo.toml file and a src directory with a main.rs file inside.
 It has also initialized a new Git repository along with a .gitignore file.
Cargo.toml file is in the TOML (Tom’s Obvious, Minimal Language) format, which is Cargo’s configuration format.
Cargo.toml describes the project’s metadata, such as the

1. Project’s name,
2. Its version, and
3. Its dependencies.
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"

[dependencies]

 The first line, [package], is a section heading that indicates that the following statements are configuring a
package.
 In Rust, packages are referred to as crates.
### Building and Running a Cargo Project ###
$ cargo build
$ cargo run
$ cargo check
$ cargo build --release

block3 /root/rust # cargo new hello_world


Created binary (application) `hello_world` package
block3 /root/rust # tree hello_world
hello_world
├── Cargo.toml
└── src
└── main.rs

1 directory, 2 files
block3 /root/rust/hello_world # cargo run
Compiling hello_world v0.1.0 (/root/rust/hello_world)
Finished dev [unoptimized + debuginfo] target(s) in 1.74s
Running `target/debug/hello_world`
Hello, world!
block3 /root/rust # tree hello_world
hello_world
├── Cargo.lock
├── Cargo.toml
├── src
│ └── main.rs
└── target
├── CACHEDIR.TAG
└── debug
├── build
├── deps
│ ├── hello_world-41309fa5631cf679
│ └── hello_world-41309fa5631cf679.d
├── examples
├── hello_world
├── hello_world.d
└── incremental
└── hello_world-piieziagd64g
├── s-gf11eev05t-b8gy7z-3dgcwcgxcvw94
│ ├── 17shh8yjp31t9tyr.o
│ ├── 1b6ydeni2b79s1xb.o
│ ├── 1oobr0kbynjc5z0d.o
│ ├── 2d12tio9maj7d8ty.o
│ ├── 2oj6d9jlshlxum4j.o
│ ├── 3j7hh9aw5aybsehd.o
│ ├── 47gw2ue69elo3h9v.o
│ ├── 4c2488d0zcqpcvb4.o
│ ├── dep-graph.bin
│ ├── query-cache.bin
│ └── work-products.bin
└── s-gf11eev05t-b8gy7z.lock

9 directories, 20 files

Cargo.lock is a file that specifies the exact version numbers of all the dependencies so that future builds
are reliably built the same way until Cargo.toml is modified.
One of the first things that you are likely to notice is that strings in Rust are able to include a wide range of
characters.
Strings are guaranteed to be encoded as UTF-8
Macros can be thought of as fancy functions for now. These offer the ability to avoid boilerplate code.
In the case of println!, there is a lot of type detection going on under the hood so that arbitrary data types can
be printed to the screen.

Downloading the book’s source code


sources:
https://manning.com/books/rust-in-action
https://github.com/rust-in-action/code
ch1-penguins

fn main() { // <1> <2>


let penguin_data = "\
common name,length (cm)
Little penguin,33
Yellow-eyed penguin,65
Fiordland penguin,60
Invalid,data
";

// The backslash (\) allows the string to span multiple lines without inserting newlines.

let records = penguin_data.lines();

// This converts the penguin_data string into an iterator of lines using the lines() method,
where each line of text becomes a separate element in the iterator.

for (i, record) in records.enumerate() {

// The enumerate() method is used to return both the index (i) and the line (record), as a tuple

if i == 0 || record.trim().len() == 0 { // <3>

// The .trim() function in Rust removes any leading and trailing whitespace (spaces, tabs,
newlines, etc.) from a string slice.

continue;
}

let fields: Vec<_> = record // <4>

// This defines a vector fields, which will store the split components of the record (a line from
penguin_data).

.split(',') // <5>

// This splits the record string into parts based on commas (,). For example, "Little penguin,33"
becomes ["Little penguin", "33"].

.map(|field| field.trim()) // <6>

// This map() function applies a closure to each field resulting from the split(). The closure
trims any leading or trailing whitespace from each field.

.collect(); // <7>

// The collect() function gathers the processed fields into a Vec<_>, which becomes a vector
fields containing the split and trimmed values of each line.

if cfg!(debug_assertions) { // <8>

//This checks if the code is being compiled in debug mode. cfg!(debug_assertions) evaluates to
true only when debug assertions are enabled (e.g., when compiling with cargo build --debug).

eprintln!("debug: {:?} -> {:?}",


record, fields); // <9>
}

let name = fields[0];


if let Ok(length) = fields[1].parse::<f32>() { // <10>

// This attempts to parse the second element of fields (the length) as a floating-point number
(f32). The if let Ok(length) syntax checks if the parsing succeeds (Ok), and if so, it assigns
the result to length.

println!("{}, {}cm", name, length); // <11>


}
}
}

Listing 1.3
rustc --explain E0382

You might also like