let thread : &'static Rust = "The Rust Thread".into()

QuadDamaged

Ars Praefectus
3,955
Subscriptor++
If you want to have someone to ping ideas on Rust topics I have had good convos on these discords:


- Rust Programming Language Community (mostly users, laid back but quite technical) at https://discord.com/invite/rust-lang-community
- Rust Programming Language (more serious, focuses around improving the language / coordinating efforts) at https://discord.gg/rust-lang
 

Thegn

Ars Legatus Legionis
13,121
Subscriptor++
how would I create a vector of structs, and once that is done, to add to each element within each struct?
I could be VERY wrong here... (Just an amateur)
Code:
let theVec: mut Vec<StructType> = Vec::new();

theVec.push(StructType); // For each struct needing to be added.
You define the struct, then add it.

There's also the vec! macro:
Code:
let mut xs = vec![1i32, 2, 3];
Might not be useful in this case.

For iterating through, use for i in v:
Code:
    let mut v = vec![100, 32, 57];
    for i in &mut v {
        *i += 50;
    }

Some light reading:
https://doc.rust-lang.org/stable/rust-b ... d/vec.html
https://doc.rust-lang.org/stable/book/c ... ctors.html
 
how would I create a vector of structs, and once that is done, to add to each element within each struct?
I could be VERY wrong here... (Just an amateur)
Code:
let theVec: mut Vec<StructType> = Vec::new();

theVec.push(StructType); // For each struct needing to be added.
You define the struct, then add it.

There's also the vec! macro:
Code:
let mut xs = vec![1i32, 2, 3];
Might not be useful in this case.

For iterating through, use for i in v:
Code:
    let mut v = vec![100, 32, 57];
    for i in &mut v {
        *i += 50;
    }

Some light reading:
https://doc.rust-lang.org/stable/rust-b ... d/vec.html
https://doc.rust-lang.org/stable/book/c ... ctors.html


Ah, yeah, trying to replicate something I did in C++ in Rust, and since linked lists aren't a thing really with no null pointers, I'm thinking having the user input how many structs, then the items for each struct, and only once that is done, add it to a vector.

I'll toy around with the idea more today.
 

Lt_Storm

Ars Praefectus
16,294
Subscriptor++
how would I create a vector of structs, and once that is done, to add to each element within each struct?
I could be VERY wrong here... (Just an amateur)
Code:
let theVec: mut Vec<StructType> = Vec::new();

theVec.push(StructType); // For each struct needing to be added.
You define the struct, then add it.

There's also the vec! macro:
Code:
let mut xs = vec![1i32, 2, 3];
Might not be useful in this case.

For iterating through, use for i in v:
Code:
    let mut v = vec![100, 32, 57];
    for i in &mut v {
        *i += 50;
    }

Some light reading:
https://doc.rust-lang.org/stable/rust-b ... d/vec.html
https://doc.rust-lang.org/stable/book/c ... ctors.html


Ah, yeah, trying to replicate something I did in C++ in Rust, and since linked lists aren't a thing really with no null pointers, I'm thinking having the user input how many structs, then the items for each struct, and only once that is done, add it to a vector.

I'll toy around with the idea more today.

So, there may be no null pointers, but there is an Option type. It serves the same basic purpose.
 
So I am having a brain fart here. I'm starting a little pet project, and just seeing how I can print out stars. I threw into a function to get input from a user, turning that string into an integer, and then wanting to use that integer in a my fn print_stars().

However, for some reason, I can't remember how to feed the return value of one function, into another function, the error I am getting is "Note: expected type {integer} found fn pointer fn() -> i32"

Function:

fn get_input() -> i32 { println!("How many stars would you like to print? "); let mut line = String::new(); std::io::stdin().read_line(&mut line).unwrap(); let number : i32 = line.trim().parse().unwrap(); return number ; }

I haven't been using Rust since the summer so please keep the name calling to a minimum.
 
Last edited:

tb12939

Ars Tribunus Militum
1,797
So I am having a brain fart here. I'm starting a little pet project, and just seeing how I can print out stars. I threw into a function to get input from a user, turning that string into an integer, and then wanting to use that integer in a my fn print_stars().

However, for some reason, I can't remember how to feed the return value of one function, into another function, the error I am getting is "Note: expected type {integer} found fn pointer fn() -> i32"

Function:

fn get_input() -> i32 { println!("How many stars would you like to print? "); let mut line = String::new(); std::io::stdin().read_line(&mut line).unwrap(); let number : i32 = line.trim().parse().unwrap(); return number ; }

I haven't been using Rust since the summer so please keep the name calling to a minimum.
Gonna need to see more context code. I'd guess you're using it as a function value rather than calling it - e.g. using get_input vs get_input()
 
Last edited:
Gonna need to see more context code. I'd guess you're using it as a function value rather than calling it - e.g. using get_input vs get_input()
Motherfucker, that was it. I can't believe I missed those parantheses. Thank you sir.

So what I want to do is make a Rust program that can read the raw bytes from a network interface, display it on the screen on the form of a row of stars, that changes color as it reads the bytes and reaches the read bandwidth limit of whatever interface is being read. This is just the first part to make sure I can get some output from the function I plan on reading the byte data into. So now, I can print stars!
 
Getting fun here....

So depending on the amount of stars you choose to print, I wrote this:

Code:
fn print_stars(x: i32) {
let mut i = 0;
    while i < x {
if x <= 25 { print!("{}", format!("*").green()) }
else if x > 25 && x <= 76  { print!("{}", format!("*").yellow()) }
else if   x > 76 { print!("{}", format!("*").red().bold()) }

i = i+1;

    }

The first if statement prints green stars, but another after that, it is not checking the else if statements. Any idea why? Again, this is just mindless hobby crap, no biggie if no one answers. I'll get it figured out eventually.
 
Last edited:

tb12939

Ars Tribunus Militum
1,797
The first if statement prints green stars, but another after that, it is not checking the else if statements. Any idea why? Again, this is just mindless hobby crap, no biggie if no one answers. I'll get it figured out eventually.
Is it working as expected?

As currently written the 'if/else chain' depends only on the input 'x', not the increasing 'i', so the colour decision is effectively done once for the whole 'bar'. If you want a multi-colour bar, you'd need to use 'i'
 
Is it working as expected?

As currently written the 'if/else chain' depends only on the input 'x', not the increasing 'i', so the colour decision is effectively done once for the whole 'bar'. If you want a multi-colour bar, you'd need to use 'i'
I changed it to match statements:

Code:
fn print_stars(x: i32) {
    let mut i = 0;
    while i < x {
        match x {
            1..=25 => {
                print!("{}", "*".green().italic())
            }
            26..=75 => {
                print!("{}", "*".underline().cyan())
            }
            76..=10000 => {
                print!("{}", "*".red().bold())
            }

            _ => {
                print!("Thats a lot of stars")
            }
        }


        i = i + 1;
    }

But yes, I would eventually like to have the stars increase so the first 25 are green...yellow...red as it reaches the higher numbers. But right now I've moved over to reading about network interfaces and how to get the data (or raw byte data) so I can see how best to see once read, divide that into the speed of the interface, to then print stars on the data being recorded. Some of the documentation for the network interfaces in Rust leave a lot to be desired.
 

Rubel

Ars Scholae Palatinae
842
Are there any big-name apps or websites written in Rust? Eg Github uses Ruby and Erlang, and pandoc uses Haskell.
One of the more interesting uses of Rust comes from cloud provider fly.io.
I enjoy the answers to "why Rust" folks come up with. It's fun to see how not everything needs to be in Golang.
 
So I found Rustlings as a tool to get you acclimated to the rust language using pretty easy/rudimentary programming tasks and I am finding it quite enjoyable. https://github.com/rust-lang/rustlings

I've been self teaching for a while now so I'm flying through it but its making me interested in picking up my network monitoring project I tasked myself with doing last year. A few weekends and if I find a decent GUI library to use I could get it done in a few weeks. At least if I decide to forgo leaving it a CLI program....

I recommend it for anyone that wants to see what the language is about.
 
  • Like
Reactions: ArmoredCow

koala

Ars Tribunus Angusticlavius
7,579
So I had a rough Thursday at work and...

I'm making a small K8S app, and writing the K8S manifests to deploy that was... frustrating. IMHO, you need a high level abstraction on them for most purposes. I looked at a few different options (Tanka/Jsonnet, Dhall, etc.), but none felt right. I then turned to Python, but the K8S "models" available are not very satisfactory to work with either...

And as I wanted to "indulge myself" after Thursday, I started poking at writing my stuff in Rust. Dang, it's completely the wrong language for that... but... it's better than anything else? Yes, it's harder than it could be on other languages, but kube/k8s-openapi typing is very good... and for once, my editor is starting to work very well with Rust code. I suck at Rust, so I'm struggling to find the correct patterns (and it's likely what I'm writing won't be reusable, etc.), but OTOH, it's been really pleasant to write..

I've created 6 of the 7 K8S resources I need, and it's definitely https://xkcd.com/1319/ ... but at least I'm having fun and "treating myself" :D
 

koala

Ars Tribunus Angusticlavius
7,579
I think I'll make my code public today. Just a few random notes:
  • It's definitely worth it to play with crate features. My lib crate goes from ~43s to ~26s of build time by disabling default features, which in this case I don't need. One of my big "reasons not to use Rust" in this project was slow build times (this is basically "scripting" which I could do in bash or Python, so paying for builds is not great). It's still more friction to build than if I had used a more "suitable" way to write this, but it's not as much as I expected.
  • Maybe rust-analyzer is improving a lot? I'm using it with lsp-mode on Emacs, and previously it was slow and unreliable. I'm either getting more comfy with Emacs, or perhaps this project has much less dependencies that other stuff I've worked with... or rust-analyzer is much better than a couple releases ago. It was quite pleasurable to get autocompletion for K8S resource fields, etc. Also automatic format on save works by default, error highlighting works well, completion adds use statements automatically, and code actions work well. It's getting close to the Eclipse Java experience that hooked me many many moons ago on Java.
  • I think the ergonomics about "optional/named arguments" are not as nice as Python (but then, Python has its own set of big flaws around that), but ..Default::default() while being a handful to type... well, where had you been all my life?
But this project is moving a bit the needle on me thinking Rust is suitable for general software development. There's a few too many &, as_mut, into, and clone, and I find it frustrating that using strings is "so complex"... but I think if I coded more Rust that might all become second nature. I still feel "insecure" I'll hit bumps that will be hard to overcome, or where I might get stuck, but I'm tempted to do more "low-risk" stuff in Rust, even for stuff that can work perfectly with GC, runtimes, etc.
 

koala

Ars Tribunus Angusticlavius
7,579
If you filter by the macOS tag in https://www.areweguiyet.com/ , there are two projects there that look like the "right" bindings to use for macOS GUI apps. Got 0 knowledge about macOS GUIs, though. I think in this day and age, except for stuff that deliberately targets a single platform, the mindshare is on the vast array of crossplatform GUI toolkits that are all disappointing or bad in some way.

Reminder also that you can develop the GUI in a different language (or languages) and bind to a Rust core. Flutter seems to be quite popular for combining with Rust cores, although I'm not really sure the quality of desktop support.

In any case, I think in this day and age I would design any application to have a clear API, so multiple frontends can be implemented easily- and maybe even allow running the core on one system, and the UI in other.
 

MilleniX

Ars Tribunus Angusticlavius
6,767
Subscriptor++
What is the status with regards to using Rust for native UI elements? I think I have the meat down of my network analyzer idea its constituent parts laid out, but am wondering if it is worth my time to use a GUI as opposed to just a CLI tool.
Conveniently, /r/Rust just had a post about the Slint toolkit. Looks like it covers at least Windows, macOS, and GTK, as well as some embedded options.
 

koala

Ars Tribunus Angusticlavius
7,579
Another bump in part because my next job is in a company whose main product is implemented in Rust.

I'm panicking a bit (I did try to convince them during the interview that I was not sure I was capable of becoming a good Rust programmer- but I think they are all people with extensive C/C++ backgrounds and they don't really get where I'm coming from), so I'm trying to get into a rhythm.

And I think I chose badly. I decided to toy with a side project that uses https://github.com/facebook/starlark-rust and... I'm moving forward, but by replicating the code in their tests to figure out the complex (to me) typing that this library requires.