What will be result?
A. Both X & Y will give error
B. Y will give error & X will print "Kernel" and "Astra"
C. X will print "Kernel" & Y will print "Kernel" and "Astra"
D. both prints "Kernel" & "Astra"
(it was shared in ThePrimeagen stream by one of viewers)
Will it print Hello World?
A. No, it will give compilation error
B. Yes, it will print `Hello World`
C. No, it will panic at runtime but prints 1
This challenge was brought to you by @tuxmain
@AstraKernel I have a #RustChallenge for you.
What will be the output of the following code:
- A) nothing, it's not valid rust code
- B) nothing, it will crash runtime
- D) something nice
- E) undefined behavior
#rust #cpp #unsafe #gnu #crablang #imsorry #nice
What will be the output in "Nightly"?
A. Compilation error (already mentioned nightly so it might run, but will you trust me? )
B. prints "return"
C. prints "!"
D. prints nothing
It's been a long time since I posted (found this in discord)
What is the output?
A. Both X and Y prints "True" and "False"
B.
Only Y prints "True" and "False"
X just prints "False"
C. Gives compilation error
D. Runtime error
i know i know, sorry... :P
What is the output?
A. Prints memory address 3 times
B.
"Rust"
Memory Address
Memory Address
C.
"R"
"u"
"st"
D.
"Rust"
"Rust"
"Rust"
Rust challenge explained
Ans: C. It never reaches the prints statement. It goes in an infinite loop
But, how? Let's break down
End value is 0xfffffff which is 268435455.0. it is not very important here. So let's remove it and simplify the code with a constant 20,000,000.0 end value
`f` value stops increasing(round upped) after value 16777216.0 when you add just 1.0
To know why,you have to understand how the f32 is stored. Float is not stored as simple as integer
f32 uses IEEE 754 format to store the value
Larger f32 number loses accuracy
In a 32 bit float (any language not just rust):
1st bit is used for sign identification (positive or negative)
Following 8 bits used for exponent
Next 23 bits used for storing mantissa
You can see more here, how it is calculated
https://youtu.be/8afbTaA-gOQ
(Continued )
an Interesting #RustChallenge given by @sombrastudios
What will happen?
A) prints "Hello World" instantly
B) prints "Hello World" after 3 seconds
C) never reaches the print statement
D) Compilation error
What will be the result?
A. Runtime error
B. Compile time error
C. It won't give an error and does nothing
Credits : @musicmatze
What is the output?
A. It will print Kernel
B. It will give a compilation error (explain why)
C. It will give a runtime error (explain why)
What is the output of this?
A. Error
B. RustJsPython
C. (Rust, Js, Python)
D. Rust
(Not hard/tricky or unique as #RustChallenge, just simple quizzes)
Fix the following code
Error: "cannot borrow `x` as mutable because it is also borrowed as immutable"
Playground link:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=b13fc771b4c2ea39d490375c54fd5a4d
You can answer either with fixed playground link or just explanation (No poll)
#RustChallenge without semicolons it's funnier;
Find what this algo is for!
let f = |mut x: u32, mut y: u32| std::iter::once(((), 0)).chain((1..).map_while(|g| Some(((x,y)=((x%2==0).then_some(x>>1)?,(y%2==0).then_some(y>>1)?), g)))).last().map(|(_, g)| (|z: &mut u32| while *z&1==0 {*z /= 2}, g)).map(|(h, g)| (while x != 0 {match (h(&mut x), h(&mut y), x.cmp(&y)).2 {std::cmp::Ordering::Less => y = (y-x)/2, _ => x = (x-y)/2}}, y<<g).1).unwrap();
Hint: use irust and cargo fmt
What is the result?
A. Error
B. Prints 2 4 6 8
C. Prints 2
D. Prints 2 6 8
Vote here
Underscore expressions in Rust
Underscore `_`, a reserved identifier - Anything passed to into it will be ignored(But careful, it is not dropped)
Underscore doesnt copy,move or borrow the value
In the following code, we create String instance 'x'.We pass it to the underscore which does nothing than ignoring whatever we give. It does not move the ownership.
Ownership still remains with 'x'
let x = String::new("Kernel");
let _ = x;
Then what is the use?
It will be useful in situation, when you are not interested in a certain value and ignore it.
For example:
fn main(){
let x = (21, "Parker");
let (age, _) = x; //Ignore the name, only want age from the tuple.
println!("age: {}", age);
}
Resources:
https://runrust.miraheze.org/wiki/Underscore
Underscore is not exactly "throw away" but rather it is exactly "don't bind to variable:
https://news.ycombinator.com/item?id=22449960
https://www.reddit.com/r/rust/comments/96zxge/underscore_does_not_bind_codechain/