Code
Hub
Workspaces
Following
Trending
Connect
MCP
copy
Create free account
hub
/
github.com/RalfJung/rust-101
/ types & classes
Types & classes
36 in github.com/RalfJung/rust-101
⨍
Functions
176
◇
Types & classes
36
↓ 1 callers
Class
ConcurrentCounter
solutions/src/counter.rs:6
↓ 1 callers
Class
ConcurrentCounter
src/part15.rs:38
Interface
Action
So, let us define a trait that demands that the type provides some method `do_action` on digits. @ This immediately raises the question: How do we pas
src/part10.rs:17
Class
BigInt
solutions/src/bigint.rs:22
Class
BigInt
@ To write this down in Rust, we use a `struct`, which is a lot like structs in C: @ Just a bunch of named fields. Every field can be private to the c
src/part05.rs:25
Class
Callbacks
solutions/src/callbacks.rs:5
Class
Callbacks
@ So, what can we do, if we can't store the callbacks in a vector? We can put them in a box. @ Semantically, `Box<T>` is a lot like `T`: You fully own
src/part11.rs:42
Class
Callbacks
src/part12.rs:27
Class
CallbacksMut
src/part12.rs:125
Class
CallbacksV1
@ First of all, we need to find a way to store the callbacks. Clearly, there will be a `Vec` @ involved, so that we can always grow the number of regi
src/part11.rs:13
Class
DropChecker
solutions/src/list.rs:186
Class
Iter
@ In writing this down, we again have to be explicit about the lifetime of the reference: We @ can't just have an `Iter`, we must have an `Iter<'a>` t
src/part09.rs:30
Class
IterMut
solutions/src/list.rs:113
Class
IterMut
@ For Rust to accept the type, we have to add two more annotations. First of all, we have to @ ensure that the data in the list lives at least as long
src/part16.rs:152
Class
Line
solutions/src/rgrep.rs:20
Class
LinkedList
solutions/src/list.rs:19
Class
LinkedList
The linked list itself stores pointers to the first and the last node. In addition, we tell Rust that this type will own data of type `T`. @ The type
src/part16.rs:57
Interface
Minimum
This trait is used to compute the minimum of two elements of the given type
solutions/src/vec.rs:50
Interface
Minimum
solutions/src/bigint.rs:5
Interface
Minimum
@ So, as a first step towards a generic `vec_min`, we define a `Minimum` trait. @ For now, just ignore the `Copy`, we will come back to this point lat
src/part02.rs:73
Interface
Minimum
With our new knowledge of lifetimes, we are now able to write down the desired type of `min`: @ We want the function to take two references *with the
src/part07.rs:10
Class
Node
solutions/src/list.rs:12
Class
Node
@ As usually, we start by defining the types. Everything is parameterized by the type `T` of the @ data stored in the list. A node of the list consist
src/part16.rs:35
Enum
NumberOrNothing
A number, or nothing
solutions/src/vec.rs:5
Enum
NumberOrNothing
An `enum` for "a number or nothing" could look as follows:
src/part00.rs:19
Enum
NumberOrNothing
@ And the same applies to case distinction with `match`: Every `arm` of the match @ gives the expression that is returned in the respective case. @ (W
src/part01.rs:26
Class
Options
solutions/src/rgrep.rs:14
Class
Options
src/part13.rs:29
Enum
OutputMode
solutions/src/rgrep.rs:7
Enum
OutputMode
src/part13.rs:22
Interface
Print
solutions/src/vec.rs:160
Interface
Print
Exercise 03.1**: The goal is to write a generic version of `SomethingOrNothing::print`. To this end, define a trait `Print` that provides (simple) gen
src/part03.rs:115
Class
PrintWithString
@ As the next step, we need to come up with some action, and write an appropriate implementation @ of `Action` for it. So, let's say we want to print
src/part10.rs:38
Enum
SomethingOrNothing
A polymorphic (generic) "some value, or no value"
solutions/src/vec.rs:42
Enum
SomethingOrNothing
@ The solution to this is called *generics* or *polymorphism* (the latter is Greek, @ meaning "many shapes"). You may know something similar from C++
src/part02.rs:15
Enum
Variant
## Mutation + aliasing considered harmful (part 2) @ Now that we know how to create references to contents of an `enum` (like `v` above), there's @ an
src/part05.rs:125