• Welcome to the new COTI server. We've moved the Citizens to a new server. Please let us know in the COTI Website issue forum if you find any problems.

Rust

Leitz

SOC-14 1K
Admin Award
Baron
The language, not the coloration of your two century old free trader.

I needed a mental break, so I poked at Rust a little. Here's code that generates a set of 6 numbers, from the computer roll of 2d6.

Code:
// Really basic Rust. Get rust via 'rustup', then create a new project with
//      cargo new chargen
//
// Then go into the chargen directory and edit src/main.rs as below.
//
// After that, run it to see if it works.
//      cargo run
//
// Then build it.
//      cargo build


// Imported via the Cargo.toml file
use rand::Rng;

// Returns an 8 bit unsigned integer.
// Like Ruby, the last successful command is the return value.
fn roll() -> u8 {
    rand::thread_rng().gen_range(1..7)
}

// 2d6 for us Traveller people. Make your own 3d6 for DnD.
fn two_d6() -> u8 {
    roll() + roll()
}

// The main function is where things happen.
fn main() {
    // 'mut' means i is mutable, otherwise it would not be.
    let mut i = 0;
    while i < 6 {
        i = i + 1;
        // The ! means print is a macro. Still learning about those.
        print!("{} ", two_d6());
    }
    // This is to add a newline after the results. If you're doing one line,
    // you can get a free newline with println!.
    //     println!("My cool character.")
    print!("\n")
}

// Here's a test function. The "#[test]" is the declaration.
// It is called with:
//      cargo test
#[test]
fn test_roll() {
    let mut counter = 0;
    // a is a range, and the end of the range is not in the range.
    let a = 1..7;
    // Run this test 100 times.
    while counter < 100 {
        counter = counter + 1;
        assert!(a.contains(&roll()));
    }

}
 
when I do dice rollers, I usually pass in 2 params: the number of dice and the number of faces. so that roll(die=6, number=2) gives 2 d6 rolls and totals. Just make sure you actually roll each die (random number) to approach the same curve. And also., depending on the language and things, the random number process may not always be random and you may want to seed it or something to give it a better chance to be random. Rust may do this behind the scenes for you.

and having worked with Ruby - I have gotten used to that "last thing you do is the return value" and like it. Ruby does allow you to also return multiple values, so fun things like a, b = some_function() can assign those 2 variables assuming you do a return something, something else

I do like writing code...

note: sometimes I actually add other parameters for the boon/bane or other DMs. depending on what I am using it for.
 
Yup, I usually do "num_dice" and "modifier", with the latter being either positive or negative. With Rust, I'm just starting out, like, "first few pages of the book" starting out, and I hunted down how to do the PRNG and loop. I stopped when I had the working model; I've made the mistake of running myself into the ground trying to code "one more thing".

I'm already thinking about connecting it to a SQLite DB and formatting the chargen results. Could probably expose it on a port as a network microservice, too. :D
 
Rust is an interesting language. It attempts to be C/C++ but with memory safety turned to 11. It largely succeeds with this, but causes you to re-think your approach to managing data. It also leans heavily into being a functional language (vs a procedural language like Java, C, or Python) which, if you are not used to writing code that way, can make a additional hurtle to learn.
 
Here’s code that generates a set of 6 numbers, from the computer roll of 2d6.
The while loops could be made more concise as for loops, e.g.

Code:
let mut counter = 0;
while counter < 100 {
    counter = counter + 1;
    // …
}

could also be written as

Code:
for counter in 1..100 {
    // …
}
 
Here's what dissuades me from Rust.

On my machine:

Code:
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time:  3.452 s
Finished at: 2023-11-18T15:45:55-08:00
------------------------------------------------------------------------

That's 12K lines of code, about 100 Java files. mvn clean and build. When I really need to iterate, I use units test which run pretty much instantly.

As I understand it, Rust build times are glacial, along with Swift (my friend, an iOS dev, constantly complained about how the Swift compiler was always spinning up the fans on his laptop). I honestly don't hear good things about modern C++ either.

I'm sure they're trying to improve that, but right now, their notoriety precedes them. I'm not that patient.

That infamous XKCD "Compiling!" just doesn't really apply to my work, most of the time.
 
I'm okay with slow compile if fast execution. Much better than fast compile (or not even compile for a lot of languages now) and slow run time. Looking at you, Rails (though that is mostly the fault of developers who really don't understand good software design: Rails is an opinionated framework and I disagree with a few of those opinions! But if written with care, it can be a fast system. Just few care)

Years ago, my first real job had a script that had to compile all the programs (mostly PL/B and a few COBOL). It would take about 2 hours to compile them all. Flash forward a few years and the OS (RMS/XA) was ported from the Datapoint hardware to Intel. Compile took about 20 minutes for the roughly same number of programs.

Of course, my boss, the owner, still went to the bar when we started compiling even though it would be done before he got his first beer.
 
As I understand it, Rust build times are glacial, along with Swift (my friend, an iOS dev, constantly complained about how the Swift compiler was always spinning up the fans on his laptop). I honestly don't hear good things about modern C++ either.
Rust does a lot of error checking during compile time, so yeah, it's probably slower than C in that regard. If it does compile, you know it's good! The other big win is that it eliminates a lot of memory handling bugs, and is almost C fast. As tjoneslo hinted at above, it may have a steep learning curve, depending on your coding background.
 
Kakistocrat, I used your "counter" fix, thank you!
For the rest of you, PLEASE FOR THE LOVE OF AUNT JEMIMA DON'T CONSIDER THIS BEST PRACTICE CODE!

I got side-tracked from Rust shortly after my first post here, but now I have some time to get back to it.


It generates a basic character in the format:

Code:
Fred Flintstone [M] (age 57) [798434]
 
Back
Top