DEV Community

Cover image for A hundred ways to say “Hello!”
Antonov Mike
Antonov Mike

Posted on

A hundred ways to say “Hello!”

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
Enter fullscreen mode Exit fullscreen mode

And this is how each sub-project is connected in Cargo.toml

[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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

fn main() {
    println!("{}", include_str!("text.txt"));
    println!("{:?}", include_bytes!("text.txt"));
}
Enter fullscreen mode Exit fullscreen mode

text.txt

External text.txt file
Enter fullscreen mode Exit fullscreen mode

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]
Enter fullscreen mode Exit fullscreen mode

2. Hello args!

Using command line arguments. Very interesting topic. Maybe I am gonna look at it closer someday.

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);
}
Enter fullscreen mode Exit fullscreen mode

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!
Enter fullscreen mode Exit fullscreen mode

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

mod print;
fn main() {
    print::run();
}
Enter fullscreen mode Exit fullscreen mode

print.rs

pub fn run() {
    println!("Hello, print.rs file");
}
Enter fullscreen mode Exit fullscreen mode

cargo run --bin hello_from_print_file

Hello from print.rs file
Enter fullscreen mode Exit fullscreen mode

4. GTK and message_dialog

Example of floating window "hello world" using GTK

[dependencies]
gtk = "0.15.0"
Enter fullscreen mode Exit fullscreen mode

main.rs

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();
}
Enter fullscreen mode Exit fullscreen mode

cargo run --bin message_dialog

message_dialog

5. Bonus: println! options

And a little extra print options

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);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)