Q. What is the purpose of the 'cargo' tool in Rust?

  • (A) Memory management
  • (B) Code formatting
  • (C) Package management and building
  • (D) Debugging
πŸ’¬ Discuss
βœ… Correct Answer: (C) Package management and building
Explanation: 'cargo' is Rust's package manager and build system.

Q. Which of the following is a valid way to print to the console in Rust?

  • (A) echo("Hello")
  • (B) print("Hello")
  • (C) println!("Hello")
  • (D) console.log("Hello")
πŸ’¬ Discuss
βœ… Correct Answer: (C) println!("Hello")
Explanation: Rust uses the macro 'println!' to print to the console.

Q. What is the result of the following code?

Code:
fn main() {
    let s = String::from("hello");
    let t = s;
    println!("{}", s);
}
  • (A) hello
  • (B) Compilation error
  • (C) Undefined behavior
  • (D) Nothing
πŸ’¬ Discuss
βœ… Correct Answer: (B) Compilation error
Explanation: Ownership of 's' is moved to 't', so 's' is no longer valid.

Q. Which keyword is used to return a value from a function in Rust?

  • (A) return
  • (B) yield
  • (C) break
  • (D) exit
πŸ’¬ Discuss
βœ… Correct Answer: (A) return
Explanation: Rust uses 'return' to return values from functions.

Q. What is the purpose of the 'Result' type in Rust?

  • (A) To store strings
  • (B) To handle errors
  • (C) To define structs
  • (D) To manage memory
πŸ’¬ Discuss
βœ… Correct Answer: (B) To handle errors
Explanation: 'Result' is used for error handling and represents success or failure.

Q. Which of the following is a valid way to define a struct in Rust?

  • (A) struct MyStruct { name: String }
  • (B) class MyStruct { name: String }
  • (C) type MyStruct = { name: String }
  • (D) object MyStruct { name: String }
πŸ’¬ Discuss
βœ… Correct Answer: (A) struct MyStruct { name: String }
Explanation: Rust uses the 'struct' keyword to define custom data types.

Q. What is the output of this code?

Code:
fn main() {
    let x = vec![1, 2, 3];
    println!("{}", x[1]);
}
  • (A) 1
  • (B) 2
  • (C) 3
  • (D) Compilation error
πŸ’¬ Discuss
βœ… Correct Answer: (B) 2
Explanation: Indexing starts at 0, so x[1] is 2.

Q. Which keyword is used to implement traits in Rust?

  • (A) trait
  • (B) impl
  • (C) interface
  • (D) define
πŸ’¬ Discuss
βœ… Correct Answer: (B) impl
Explanation: Rust uses 'impl' to implement traits for types.

Q. What does the 'unwrap()' method do on a Result type?

  • (A) Returns the error
  • (B) Returns the value or panics
  • (C) Ignores the value
  • (D) Converts to Option
πŸ’¬ Discuss
βœ… Correct Answer: (B) Returns the value or panics
Explanation: 'unwrap()' returns the value if Ok, or panics if Err.

Q. Which of the following is a macro in Rust?

  • (A) print
  • (B) println!
  • (C) format
  • (D) debug
πŸ’¬ Discuss
βœ… Correct Answer: (B) println!
Explanation: Macros in Rust are marked with a '!' like 'println!'.