Files
2026-08-04 06:57:36 -06:00

121 lines
2.7 KiB
Markdown

### 1. Modules and Imports
```rust
mod multiset_operations;
mod set_operations;
use multiset_operations::MultiSet;
use set_operations::Set;
```
- `mod`: Declares a module. The actual code for the modules `multiset_operations` and `set_operations` is not shown, but they should be either in the same file or in `multiset_operations.rs` and `set_operations.rs` files respectively.
- `use`: Imports types or functions from a module into the current scope. Here, `MultiSet` and `Set` are imported.
### 2. Main Function
```rust
fn main() {
// ...
}
```
- `fn`: Keyword to define a new function.
- `main`: The entry point of a Rust program.
### 3. Variable Declaration and Initialization
```rust
let a = Set::from_vec(vec![true, false, true, false, true, false, true, false, true, false]);
```
- `let`: Used for variable declaration.
- `Set::from_vec`: Calls a associated function (similar to a static method in other languages) named `from_vec` of the `Set` struct.
### 4. Mutable Variable Declaration
```rust
let mut a_mult = MultiSet::from_vec(vec![3, 2, 0, 0, 0, 0, 0, 0, 0, 0]);
```
- `mut`: Indicates that the variable is mutable, i.e., its value can be changed.
### 5. Array/Vector Indexing
```rust
a_mult.elements[0] = 3;
```
- Elements in a vector/array can be accessed using the index.
### 6. Printing to Console
```rust
println!("Set A:");
```
- `println!`: A macro (not a function) to print to the console with a newline at the end.
### 7. Method Calls on Structs
```rust
let not_a = a.complement();
```
- Methods are called on instances of structs using the dot notation.
### 8. Public Struct Declaration
```rust
pub struct Set {
pub elements: Vec<bool>,
}
```
- `pub`: A visibility modifier that makes the struct and its fields public.
- `struct`: Keyword to define a new structure.
- `Vec<bool>`: A vector of boolean values.
### 9. Implementing Methods for a Struct
```rust
impl Set {
// ...
}
```
- `impl`: Begins an implementation block for methods of a struct.
### 10. Function Definitions with Parameters
```rust
pub fn union(&self, other: &Set) -> Set {
// ...
}
```
- `&self`: A reference to the current instance of the struct (similar to `this` in other languages).
- `other: &Set`: A reference to another `Set` instance.
- `-> Set`: Indicates the return type of the function.
### 11. Iterating Over Collections
```rust
for &element in &self.elements {
// ...
}
```
- `for`: Begins a for loop.
- `&element`: Pattern matching to destructure and get the value from the reference.
### 12. Printing with Format Specifiers
```rust
println!("Sum of elements in A: {}", a_mult.sum());
```
- `{}`: A placeholder that will be replaced by the value specified after the format string.