A hundred ways to say “Hello!”
Tags: rust, curgo, beginners
Published: 2022-07-27 11:40:00

## Intro
Today I will try to show you on several examples how to organize several projects in one folder. I have encountered the need for such organization when studying GTK: a lot of different examples and variants it is easier to collect in one folder and specify all the dependencies you need.

This is what the structure of the project will look like
```
hello
		-external_text_file
				-main.rs
				-text.txt
		-hello_args
				-main.rs
		-hello_from_print_file
				-main.rs
				-print.rs
		-message_dialog
				-main.rs
```
And this is how each sub-project is connected in Cargo.toml
```Rust
[dependencies]
gtk = "0.15.0"

[[bin]]
name = "external_text_file"
path = "external_text_file/main.rs"

[[bin]]
name = "hello_args"
path = "hello_args/main.rs"

[[bin]]
name = "hello_from_print_file"
path = "hello_from_print_file/main.rs"

[[bin]]
name = "message_dialog"
path = "message_dialog/main.rs"
```
Run each project
```
cargo run --bin external_text_file
cargo run --bin hello_args hello world
cargo run --bin hello_from_print_file
cargo run --bin message_dialog
```
## 1. External text.txt file
Just printing external .txt file as string and as bytes. Last one is helpful if you wanna know a char number
 
**main.rs**
```Rust
fn main() {
    println!("{}", include_str!("text.txt"));
    println!("{:?}", include_bytes!("text.txt"));
}
```
**text.txt**
```
External text.txt file
```
cargo run --bin external_text_file
```
External text.txt file

[69, 120, 116, 101, 114, 110, 97, 108, 32, 116, 101, 120, 116, 46, 116, 120, 116, 32, 102, 105, 108, 101, 10]
```

## 2. Hello args!
Using command line arguments. Very interesting topic. Maybe I am gonna look at it closer someday.
```Rust
use std::env;

fn main() {
	let arguments: Vec<String> = env::args().collect();
	let a: String = arguments[1..].join(" ").chars().collect();
	let b = arguments[1..].connect(" ");
	println!("{}", a);
   println!("{}", b);
}
```
Here we need arguments like "hello world!" or any other. They gonna be collected and printed

cargo run --bin hello_args hello world!
```
hello world!
```

## 3. External print.rs file
Simple print macro in an module. It is very important to divide your code in logical parts any time you are making something huge. For example: main.rs; gui.rs; mathematics.rs; elements.rs. Main just starts the application. Other modules contain gui logic, math algorithms and gui elements like buttons or labels.
main.rs
```Rust
mod print;
fn main() {
	print::run();
}
```
print.rs
```Rust
pub fn run() {
    println!("Hello, print.rs file");
}
```
cargo run --bin hello_from_print_file
```
Hello from print.rs file
```

## 4. GTK and message_dialog
Example of floating window "hello world" using [GTK](https://gtk-rs.org/gtk4-rs/stable/latest/book/introduction.html)

```Rust
[dependencies]
gtk = "0.15.0"
```
main.rs
```Rust
extern crate gtk;
use gtk::prelude::*;
use gtk::{ButtonsType, DialogFlags, MessageType, MessageDialog, Window};

fn main() {
    if gtk::init().is_err() {
        println!("Failed to initialize GTK.");
        return;
    }
    MessageDialog::new(None::<&Window>,
                       DialogFlags::empty(),
                       MessageType::Info,
                       ButtonsType::Ok,
                       "Hello World").run();
}
```
cargo run --bin message_dialog

![message_dialog](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ltr6paj64ysvjqgxjqsm.png)

### 5. Bonus: println! options
And a little extra print options 
```Rust
println!("{:b} {:x} {:o}", 10, 10, 10);
println!("{:?}", (10, true, "hello"));
println!("{name} is a {job}", name = ("Degrass Tyson"), job = ("scientist"));
println!("Max u8 is {}", std::u8::MAX);
println!("Max i32 is {}", std::i32::MAX);
println!("Max i64 is {}", std::i64::MAX);
println!("Max u32 is {}", std::u32::MAX);
println!("Max u64 is {}", std::u64::MAX);
println!("Max f32 is {}", std::f32::MAX);
```