Rust’s powerful type system includes the use of structs and enums, allowing developers to define complex data structures and control program flow through pattern matching. This set of 30 multiple-choice questions explores Rust’s structs, enums, and the use of pattern matching, focusing on practical aspects like struct methods, associated functions, and enums like Option and Result.
structurestructclassobjectstruct Point { x: i32, y: i32 }struct Point(x: i32, y: i32)struct { x, y }struct Point => { x, y }let p = Point.new(10, 20);let p = Point { x: 10, y: 20 };let p = new Point(10, 20);let p = Point(10, 20);:,;.#[derive] attribute in structs?
Debug or Clonestruct Info { name: String, age: u32, is_active: bool }struct Info(name, age, is_active)struct Info { name: String, age: u32, bool is_active }struct Info(String, u32, bool)pub name: Stringprivate name: Stringlet name: Stringname: String with #[allow(public)]impl block in Rust?deffunctionimplstructself in a Rust struct?fn method(self) { ... }fn method(&self) { ... }fn method(self: &mut self) { ... }fn method(self: Box<Self>) { ... }selfthissuperparentnew() method commonly defined in Rust structs?instance.method()method(instance)method->instance()instance::method()&self parameter signify when used in a method of a struct?self immutablyself mutablyselfself&mut self as the method parameterself to take ownership&self and return a new valuestatic keywordimpl blockBox keyword indicate when used in a method definition?enumenumtypetypedataSuccess and Failure?enum Result { Success, Failure }enum Result(Success, Failure)enum Result { Success => i32, Failure => i32 }enum Result {Success: i32, Failure: i32}match enum { Result::Success => { ... }, Result::Failure => { ... } }enum match { Result::Success => { ... }, Result::Failure => { ... } }match { Result => { ... } }case enum { Success, Failure }match keyword allow you to do in Rust?enum Option { Some(i32), None }enum Option { Some: i32, None: i32 }enum Option { i32 => Some, None }enum Option { Some { value: i32 }, None }match enum { Option::Some(value) => { ... } }match { Option::Some => value }match Option { Some => { value } }match Option::Some(value)else keyword_ wildcard in a match statementcontinue for unmatched patternsOptionResultEnumNullResult enum in Rust?None variant in the Option enum when pattern matching?match Option { Some(x) => { ... }, None => { ... } }match None { Option::Some => { ... }, None => { ... } }match { Option => { None } }case { None }| Qno | Answer |
|---|---|
| 1 | B) struct |
| 2 | A) struct Point { x: i32, y: i32 } |
| 3 | B) let p = Point { x: 10, y: 20 }; |
| 4 | B) Comma , |
| 5 | B) To enable automatic implementation of certain traits like Debug or Clone |
| 6 | A) struct Info { name: String, age: u32, is_active: bool } |
| 7 | A) Yes, methods can be defined inside the struct |
| 8 | A) Private |
| 9 | A) pub name: String |
| 10 | A) Tuples do not have named fields, while structs do |
| 11 | A) A function that is tied to a specific struct type but does not operate on its data |
| 12 | C) impl |
| 13 | A) fn method(self) { ... } |
| 14 | A) self |
| 15 | B) To create an instance of the struct |
| 16 | A) instance.method() |
| 17 | A) The method borrows self immutably |
| 18 | A) Use &mut self as the method parameter |
| 19 | B) Yes, by defining methods inside an impl block |
| 20 | A) The method takes ownership of a boxed value |
| 21 | A) enum |
| 22 | A) enum Result { Success, Failure } |
| 23 | A) match enum { Result::Success => { ... }, Result::Failure => { ... } } |
| 24 | B) Match patterns against specific enum variants |
| 25 | A) enum Option { Some(i32), None } |
| 26 | A) match enum { Option::Some(value) => { ... } } |
| 27 | B) Use the _ wildcard in a match statement |
| 28 | A) Option |
| 29 | A) Handling success and error in functions |
| 30 | A) match Option { Some(x) => { ... }, None => { ... } } |