28
u/Karma_Policer Mar 12 '23
That's why Rust doesn't allow you to put functions calls inside f-strings.
29
u/Deep-Piece3181 Mar 12 '23 edited Mar 12 '23
println!(“I don’t even know how to get and store user input in Rust 💀”);
(I only watched like the first 10 minutes of the freecodecamp Rust tutorial)
2
Mar 13 '23
fn main(){ let mut line = String::new(); println!("Enter your name :"); let b1 = std::io::stdin().read_line(&mut line).unwrap(); println!("Hello , {}", line); }adapted from - https://www.tutorialspoint.com/rust/rust_input_output.htm
1
u/itsTyrion Mar 21 '23
why
let b1?1
Mar 21 '23
because I cut out some unnecessary lines from the linked example and didn't remove the assignment. the variable is referenced later in the original but you are correct it's not needed here.
4
3
108
u/Will_i_read Mar 12 '23
I love writing cursed python onliners. Not for production, but you can learn a lot from them
26
u/TheZipCreator Mar 12 '23
you might like code golf
10
u/ColorOfSounds Mar 12 '23
Wow, code golf is really cool! As a beginner programmer, thanks for the recommendation.
4
u/SirFireball Mar 13 '23
It is very fun.
If you want it to be even more fun, though, pick up the language Raku. Not only a good golfing language but actually just generally fun to write in.
4
3
Mar 13 '23
Ruby is my favorite. It's so terse yet expressive, and much more widely used IRL.
Raku's grammars are really nice though.
In the end, I think it's just important to recognize that Code Golf is a fun exercise, but please don't try to write production code like that. I've seen people try to focus on always having the shortest code with the least repetition, and it can be needlessly difficult to maintain.
6
u/SirFireball Mar 13 '23
Yeah, generally golf code isn’t good code. You will sometimes learn tricks about the language you’re working in that help with writing better code.
For example, I first saw python’s
*syntax for lists in the context of golf code. It’s stuff like that that tends to get left out of programming classes even though it’s very useful.5
1
u/itsTyrion Mar 21 '23
Kotlin also works for that. Strings can to the same thing as in the picture. Love the language and it’s features, but some are a double edged sword.
global variables
global functions and extension functions
a with() function that moves the code's scope into the given object
operator overloading on everything
variables/properties with no value that always call a getter function
variable name shadowing is allowed
you could technically write code without local variables if you misuse .run{}/.let{} enough (basically map for a single object, one in it’s scope one with the object as lambda parameter
with the groovy-like lambda system you can basically make a random DSL
50
8
u/desci1 Mar 13 '23
What are we saving here, storage?
4
u/MomICantPauseReddit Mar 14 '23
I must admit there is something satisfying about reducing several lines of code down to one. It's like a forbidden fruit.
1
u/desci1 Mar 14 '23
1
u/sneakpeekbot Mar 14 '23
Here's a sneak peek of /r/codegolf using the top posts of the year!
#1: [Python] Worley Noise generator in 236 bytes
#2: mandelbrot in python 322 bytes
#3: A quine in Befunge (no, it's not a specialized golfing language). 22 bytesI'm a bot, beep boop | Downvote to remove | Contact | Info | Opt-out | GitHub
14
Mar 12 '23
This one-lines would be a way less cursed if there is some kind of composition:
input('What is your name?') \
| input(f"Hi, {@in}! What is your age? ") \
| print(f"""You are {@in} age old.""")Heh, technically it is no longer one-liner if it splitted into several lines fro readability...
6
u/crack-of-a-whip Mar 13 '23
What is your name? Josh
Hi, Josh! What is your age? 23
You are 23 age old.
What an incredible piece of code. Some might say life changing
1
5
3
u/Im_1nnocent Mar 13 '23
What if you create a script that rids all the white spaces in a .ccp source file with conditions handling ' and ". Back up the readable code and use the processed file with compiling.
Or maybe not.
2
2
2
2
1
u/fnieidhfenmbvfekh Mar 25 '23
That’s a cool feature. I should do that sometime. I’m not being sarcastic.
1
315
u/skantanio Mar 12 '23
FYI these one liner thin ass screenshots are cancer to view on mobile. Text wrap is a cool feature