Loading tutorials
PhantomData exists. You will see what a generic struct promises, why Rust rejects a type parameter that no field uses, and how to fix the error.2 min readcargo run and read compiler errors. The goal is to see that a type can exist, have a name and implement traits while using no memory. The rest of the tutorial builds on that.2 min readPhantomData fixes that.2 min readimpl blocks. It also assumes you have seen PhantomData<T> as the fix for an unused type parameter. Here you use it to attach a label to a value.2 min readPhantomData and zero-sized marker types in earlier chapters. Here you will apply them to a common bug: passing the wrong kind of ID to a function.3 min readimpl blocks. It also assumes you have met PhantomData and marker types in the earlier chapters.2 min readimpl blocks, and PhantomData as a way to use a type parameter without storing a value. It also assumes you can read a compiler error. Here you will see how one struct can offer different methods depending on its type parameter.2 min readimpl blocks, and generic structs with PhantomData marker types. It covers how a method that takes self by value turns one state of a value into another state.3 min readimpl blocks, and the marker types and PhantomData from the earlier chapters. You should also be able to read a Cargo compiler error.3 min readDoor<State> typestate from earlier chapters. It also assumes you know how pub and private modules control visibility.3 min readPhantomData marker from earlier chapters, and basic lifetimes. Until now we wrote PhantomData<T> without thinking about it. For typestate markers like Open and Closed that is fine. For generic tags such as Id<T>, the form you pick changes 3 min readDoor<State> like the one in the earlier chapters. It also assumes you can run cargo test. The goal is to turn "this call must not compile" into a test that fails if someone weakens your API.2 min readPhantomData, marker types, state-specific impl blocks, and transitions that consume self. Typestate is a tool with a price, so this chapter gives you criteria for deciding when to pay it.3 min read