[THANKS xenopeek!] RUST language: First experiments and some questions???

Chat about just about anything else
Forum rules
Do not post support questions here. Before you post read the forum rules. Topics in this forum are automatically closed 30 days after creation.
Locked
User avatar
Webtest
Level 4
Level 4
Posts: 375
Joined: Sun Feb 21, 2010 4:45 pm
Location: Carlisle, Pennsylvania, USA

[THANKS xenopeek!] RUST language: First experiments and some questions???

Post by Webtest »

Esteemed Forum Participants and Lurkers:
===============================
After reading the recent StackOverflow 2017 Programmer Survey my interest was piqued by the results for the "rust" programming language. Since then, I have successfully learned how to download and "install" it (to ramdisc) on my LM18.3 Mate 64bit LiveMedia system (see: Good Rust Tutorial Video) and enter, compile, and run a couple of "Hello world!" level programs.

Code: Select all

# Install the "rust" compiler: see referenced YouTube Video
curl -sSf https://static.rust-lang.org/rustup.sh | sh
rustc --help
rustc helloworld.rs        <-- see: Rust By Example
./helloworld
I found a "cookbook" example for a primitive multi-thread program in Rust By Example and was eager to try it. I did a little multi-threading programming for a user interface panel for an automatic HTML table generator - a table of links to files of specific interest to specific users - about 15 years ago, in Visual Basic for Applications. I still tinker with Real Time programming for some hobby controller projects - mostly using Arduino or Python on Raspberry Pi.

Although the multi-thread example ran fine, it didn't give any sense of the effects of threading, even when I 'loaded' one of the threads with a bunch of 'extra' tasks. I got the bright idea to time-stamp all of the individual tasks so that I could evaluate the threading. There is a high resolution timer that is supposed to be available in the "time crate" (library?), however, my luck ended there. I have had no luck at all implementing the "precise_time_ns" function. I figured out the basics of how the "Cargo" package manager is supposed to work, and tried the command: "cargo install -v time". It seems to try to load the library of "time" functions, but it fails miserably with the message: "error: specified package has no binaries".

I am NOT a strong systems programmer, so I don't know specifically how to proceed from here. I'm guessing that the desired functions have to be compiled by rust on my system ... somehow ... and I'm looking for some hints!

If there is anyone lurking around who has experience with rust and cargo, I would most sincerely appreciate any and all comments, suggestions, and assistance.

Blessings in abundance,
Art in Carlisle, PA USA
Last edited by LockBot on Wed Dec 07, 2022 4:01 am, edited 3 times in total.
Reason: Topic automatically closed 30 days after creation. New replies are no longer allowed.
BOAT - a hole in the water that you pour money into
LINUX - a hole in your life that you pour TIME into

HP dx2400 Core 2 Duo 8 GB. Mint 13/15/17.x/18.x Mate <on LOCKED SD cards, and Kanguru USB drives> No Hard Drive / No SSD
User avatar
xenopeek
Level 25
Level 25
Posts: 29587
Joined: Wed Jul 06, 2011 3:58 am

Re: RUST language: First experiments and some questions???

Post by xenopeek »

As this isn't Linux Mint specific moved it here. We may add a programming section to the forum in future.

I have two projects with Rust I had started on but put them on pause as I was getting slapped around by the compiler too much :) I'm working through the Programming Rust book now to get a better grasp on the concepts of the language and write more idiomatic Rust code (and hopefully get slapped around less by compiler errors for beginner mistakes).

precise_time_ns comes from the time crate. To use it you just have to add the dependency in your Cargo.toml file and in your Rust program file you have to declare you want to use that crate. Cargo will then download the time crate for you as and when you compile your program. See what to add to your Cargo.toml and Rust program file on the time crate documentation: https://doc.rust-lang.org/time/time/index.html. You can then use the time::precise_time_ns function.

While you can use rustc directly it will be a lot easier if you use cargo to build your projects because of the above. To start a new project for a program, not using version control, you would run the following (replace "programname" with the name you want to give your program):
cargo new programname --bin --vcs none
This will create a skeleton directory for your project that works with cargo. You'll find a main.rs file in the src directory. Put your program code in that. Add dependencies in the Cargo.toml file in the root of your project. Then build the program with:
cargo build
You'll find the compiled program in target/debug directory or if you added the --release option to cargo command (to build a non-debug version) you'll find it in target/release. You can also build and run it in one go with:
cargo run

I can recommend the above book (I'm half way through it). It gave me a much better grasp on concepts and why the language works as it works. I don't know how it compares to the official Rust book (https://doc.rust-lang.org/book/second-edition/) but I learn better from a book than from a website. No Starch Press are publishing the official Rust book in paper book format soon (https://nostarch.com/Rust).
Image
User avatar
Webtest
Level 4
Level 4
Posts: 375
Joined: Sun Feb 21, 2010 4:45 pm
Location: Carlisle, Pennsylvania, USA

Re: RUST language: First experiments and some questions???

Post by Webtest »

xenopeek ...
I wondered what the devil had happened to my post! At least I didn't post it in the "Newbie" section ... it seems that posts there get a LOT more responses than anywhere else. I didn't get any email notification that it had been moved [edit: Yes, I did!], but found it when I finally logged it and looked at my subscription list, which thankfully had the new link.

Thank you very much for your gracious reply. I wasn't surprised to see your avatar on the post - you had posted about trying 'rust' quite some time ago! I'm eager to try your suggestion as soon as I get the chance - it looks quite reasonable and I'll keep you posted. I'm not quite ready to spring a bunch of bucks for a new bookshelf as I'm retired and only a casual programmer - when I can escape to my lair and bang away at the keyboard.

My motto? Programming? It's more fun than crossword puzzles!
My sincere thanks ...
Blessings in abundance,
Art in Carlisle, PA USA
BOAT - a hole in the water that you pour money into
LINUX - a hole in your life that you pour TIME into

HP dx2400 Core 2 Duo 8 GB. Mint 13/15/17.x/18.x Mate <on LOCKED SD cards, and Kanguru USB drives> No Hard Drive / No SSD
User avatar
xenopeek
Level 25
Level 25
Posts: 29587
Joined: Wed Jul 06, 2011 3:58 am

Re: RUST language: First experiments and some questions???

Post by xenopeek »

Programming? It's more fun than crossword puzzles!
:mrgreen:
Image
User avatar
Webtest
Level 4
Level 4
Posts: 375
Joined: Sun Feb 21, 2010 4:45 pm
Location: Carlisle, Pennsylvania, USA

Re: RUST language: First experiments and some questions???

Post by Webtest »

WHOOPIE!!! MANY thanks xenopeek !!!

I just added the line under "[dependencies]" ...

Code: Select all

[package]
name = "mytest"
version = "0.1.0"
authors = ["mint"]

[dependencies]
time = "0.1"
... and here is my first program using a crate!

Code: Select all

// Test program named "mytest"
extern crate time;

fn main() {
    println!("\nHello, world!\n");
    
    let mut stamp = time::precise_time_ns();
    println!("stamp = {}\n", stamp);
    for n in 1..5 {
        stamp = time::precise_time_ns();
        println!("stamp {} = {}", n, stamp);
    }
}
On my Core 2 duo 1.6 GHz, the 'for' entry takes about 10 microseconds, and each iteration of the loop takes 3-4 microseconds.

Thank you SOOOO much for your GREAT assistance!!! (Also thanks to my wife sleeping in late this morning!!!)

Blessings in abundance, all the best,
Art in Carlisle, PA USA
Last edited by Webtest on Thu Mar 29, 2018 10:01 am, edited 1 time in total.
BOAT - a hole in the water that you pour money into
LINUX - a hole in your life that you pour TIME into

HP dx2400 Core 2 Duo 8 GB. Mint 13/15/17.x/18.x Mate <on LOCKED SD cards, and Kanguru USB drives> No Hard Drive / No SSD
User avatar
Webtest
Level 4
Level 4
Posts: 375
Joined: Sun Feb 21, 2010 4:45 pm
Location: Carlisle, Pennsylvania, USA

Re: [THANKS xenopeek!] RUST language: First experiments and some questions???

Post by Webtest »

If you've gotten this far in this thread ... another hint:
If you want to run the 'finished' version of the program ... no debug support ...

Code: Select all

cargo build --release
cargo run --release
If you just 'build --release' and then do 'cargo run' it will run the DEBUG version!

Blessings in abundance,
Art in Carlisle, PA USA
BOAT - a hole in the water that you pour money into
LINUX - a hole in your life that you pour TIME into

HP dx2400 Core 2 Duo 8 GB. Mint 13/15/17.x/18.x Mate <on LOCKED SD cards, and Kanguru USB drives> No Hard Drive / No SSD
Locked

Return to “Open Chat”