Share your repls and programming experiences

← Back to all posts
π in Rust
h
AmazingMech2418 (894)

I think I'm pretty much doing pi approximations in every language now, so, here it is in Rust!

Like with my approximation in Go, in order to understand the object-orientedness of the language, I used structs. Though, with Rust's safe memory system, I had to do some research to figure out mutable structs...

For anyone interested in learning Rust, as it is such a fast-growing language, think of it as almost a combination of Swift and Go, but with immutability by default. There are other parts to it, but with the basics, that is pretty much how you can explain Rust in a single sentence.

Here are the other approximations so far:
Go - https://repl.it/talk/share/p-in-Go/40950
Swift - https://repl.it/talk/share/p-in-Swift/36150
C# - https://repl.it/talk/share/p-in-C/36141
Bash - https://repl.it/talk/share/p-in-Bash/36133
Kotlin - https://repl.it/talk/share/p-in-Kotlin/36125
Ruby - https://repl.it/talk/share/p-in-Ruby/34982
Java - https://repl.it/talk/share/p-in-Java/34978
QBasic - https://repl.it/talk/share/p-in-QBasic/34973
Fortran - https://repl.it/talk/share/p-in-Fortran/34890
APL - https://repl.it/talk/share/p-in-APL/34888
Forth - https://repl.it/talk/share/p-in-Forth/34652
LOLCODE - https://repl.it/talk/share/p-in-LOLCODE/34421
Python and Node.js (Node is linked in post) - https://repl.it/talk/share/2-Approximations-of-p-In-Only-Four-Lines-of-Code/32773
C - https://repl.it/talk/share/p-Approximations-in-C/33461

Credit to @NoelBryan, @TheForArkLD , @Warhawk947 , and @LizFoster for inspiring these approximations. Most of these use the Nilakantha Series, although I also use the arctangent method (multiplying the radian arctangent of 1 by 4 to get pi and getting the arctangent with an infinite series) in three of them (Python, Node, and C).

Also, credit to @rediar for inspiring this approximation in Rust!

Next up is NASM (Assembly; only if I can actually figure it out. I might have to save it for later since it is so difficult to program in), followed by Raku (Perl 6), followed by Haskell, followed by R, followed by Dart. If you have any suggestions for after those, please let me know! I'm also thinking about making a multi-language tutorial on the transpilation of programs between languages such as what I am doing with the pi approximations.

Commentshotnewtop
Jakman (450)

One thing. Rust is more of a fusion of C and C++ without the saftey problems. You can be sure that no leaks will be made with Rust.

Jakman (450)

I told you that it is fast.

rediar (342)

Thanks, maybe save BrainF for last (jk brainf is too hard)

Warhawk947 (518)

This is amazing lol

Warhawk947 (518)

@AmazingMech2418 it's also insanely fast and accurate

AmazingMech2418 (894)

@Warhawk947 Rust is as fast as (if not faster than) C! Also, I skipped the compilation process in the .replit file to speed it up a little more since it takes some time to compile...

LizFoster (606)

Good work! None of this makes any sense to me in terms of the coding language T~T It is quite awesome nevertheless.

AmazingMech2418 (894)

@LizFoster LOL! Thank you! Rust is quite different from Python and C++... It uses structure-based inheritance instead of class-based inheritance. It is more like C and Go which also use the same object-oriented features (although C doesn't allow methods in structures and is not considered object-oriented).

LizFoster (606)

@AmazingMech2418 Interesting! I'm guessing you have a lot of fun making all of these, but which language might you do next? x-x

AmazingMech2418 (894)

@LizFoster Yes, I do. I'm thinking about doing NASM (Assembly) next based on a request on my post in Go. If that doesn't work, then Raku (Perl 6) based on another request.

LizFoster (606)

@AmazingMech2418 Nice! Also, I finally set up a facebook page where I will be posting updates on the game (if you're interested I'd love for you to check it out!), and I think it will help motivate me to stay motivated to keep working through the hard parts.

AmazingMech2418 (894)

@LizFoster Thank you! The only thing about the Facebook page thing is that I don't have Facebook, so I don't think I can see it...

LizFoster (606)

@AmazingMech2418 Ah, that's too bad. I wish there was a way to post status updates on HERE, where I can actually get feedback on my work; last time I was constantly posting new stuff I was told I should stop spamming, so... T~T

Though I'm not sure, I thought you were able to see posts even without an account; then again, I don't know and haven't really checked, so that may be wrong..

AmazingMech2418 (894)

@LizFoster Well, you could make an updates website and post the website here. Or maybe make one post and just edit it instead of reposting?

LizFoster (606)

@AmazingMech2418 Yes, both are valid ways I could do this... Or I could do this on Twitter instead. I just checked and no, you can't access Facebook without an account, but you don't need one for Twitter. Plus, I can probably get more traction there.

AmazingMech2418 (894)

@LizFoster The only issue about Twitter is that there is a character limit. (also, I don't have Twitter either)

LizFoster (606)

@AmazingMech2418 Ah, yeah, that could be a deal breaker.

LizFoster (606)

@AmazingMech2418 Oh yes, and as an update, I have completely finished inputting all of the sprite data for all 151 pokémon! I think that I will start work on either the move pool or the type effectiveness chart next..

AmazingMech2418 (894)

@LizFoster Nice! (wait, you did that image-sprite conversion 151 times?)

LizFoster (606)

@AmazingMech2418 No, 302 times :)

I had to convert both the front sprites, AND the back sprites. T~T

AmazingMech2418 (894)

@LizFoster Wow! It would probably have been a lot easier if you could just load the sprites on the start of the program since you were able to do multiple files with IDLE...

LizFoster (606)

@AmazingMech2418 Yes, it probably would have been, but I'm a masochist

:)

The IDLE is actually way more convenient, since I can use turtle, AND use Python 3 functions and syntax!

AmazingMech2418 (894)

@LizFoster Yeah. The only issue would be porting it to Repl.it. I guess you could probably get the Python 3 binary and use a Polygott repl, but I'm not sure...

LizFoster (606)

@AmazingMech2418 Maybe.. Can you use the same thing you used to get the PIL module installed to get turtle installed potentially..?

AmazingMech2418 (894)

@LizFoster Unfortunately, no. Turtle needs the GUI interface which is not supported in a normal Python repl.

AmazingMech2418 (894)

@LizFoster You could possibly figure out how to use Turtle in Tkinter though...

LizFoster (606)

@AmazingMech2418 Ah, yes! Turtle is made from tkinter anyways, so that might just work! Good idea.

Jakman (450)

I recommend all forms of C++ next.

Rovy (10)

I think my computer is glitching. every time I press run it just turns back in to the button run after 1 second when it should keep being the stop button. it just says rustc 1.36.0

Rovy (10)

ooh it worked but all it says is ./main
pi = 3.141592653589787

AmazingMech2418 (894)

@Rovy That's all it's supposed to say.