Loading tutorials
PhantomData exists. This chapter covers the rule, the reasons for it, and the first fix. Later chapters build marker types and typestate APIs on top of that fix.6 min readcargo run and read compiler output. It covers the zero-sized types (ZSTs) that the rest of the tutorial builds on. Marker types and PhantomData both depend on them.5 min readunsafe. Where they matter, the chapter explains them.5 min readPhantomData<T> used to satisfy the "parameter is never used" error. Here we look at the other half of the pairing: the types you put inside PhantomData. These are marker types, and they act as labels the compile5 min readimpl blocks, and the basics of PhantomData and zero-sized marker types from the earlier chapters. You will use them to build a typed ID, the simplest practical use of a phantom type. The ID costs nothing at runtime and turns a whole c5 min readimpl blocks, generic type parameters, and how to read compiler errors. It also assumes you have seen PhantomData<T> and zero-sized marker types from the earlier chapters. Here they are the building blocks, and the question is wh6 min readimpl blocks, and how PhantomData<T> and marker types work from the earlier chapters. Here you will use that foundation to make a method exist only when a value is in the right state. The compiler then rejects a call that does not belong, and no runt5 min readimpl blocks on generic structs, and the PhantomData marker pattern from earlier chapters. You should also be comfortable reading compiler errors. Here you will see how a method that takes self by value changes a value's type, and why that6 min readimpl blocks, and the basics of PhantomData and zero-sized marker types from earlier chapters. You should also be comfortable reading compiler errors. Here you will apply those pieces to a builder where build() does no5 min readimpl blocks, and the state types (Open, Closed) and PhantomData field from the earlier typestate chapters. Here you close the set of valid states so that nobody can add to it by accident or on purpose.5 min read'a annotations, and how PhantomData and marker types are used in typed IDs and typestate code. For typestate markers the choice of PhantomData form rarely matters. It matters for tagged values that cross threa6 min readimpl<S> Door<S> block with a close method, Door<Closed> gains a method it should not have, and every ordinary test still passes. This chapter shows5 min readPhantomData, marker types, state-specific impl blocks, consuming transitions and sealed state traits. You should be comfortable with enums, Result, and moving values in Rust.7 min read