Loading tutorials
+ or [] is shorthand for a method call on a trait. The compiler rewrites a + b into a call to a trait method and then checks the types as it would for any other call. This chapter shows the mapping, where the traits live, and which operators you can't overload. The5 min readimpl blocks, how to implement a trait, basic generics and trait bounds, ownership and the Copy trait, and associated types. The previous chapter showed that a + b is sugar for a trait method call. Here you write that trait implementation yourself 5 min readT: Trait bounds. You should also have implemented Add once, as in the previous chapter. Here the focus is the line type Output = ... and what it controls.5 min readCopy and Clone traits, generics with trait bounds, and the basic impl Add for T shape from the previous chapters. Here you will see how operator expressions interact with moves, why a + b can make a unusable, and which design 5 min readimpl Add for T, know that Output is an associated type, and understand that operators take their operands by value (covered in the previous chapters). It uses generics and trait parameters, but nothing beyond that.5 min readAdd for a struct, what the Output associated type does, and how Copy, moves and &mut borrows work. It builds on the earlier chapters on Add, Output and operand ownership.4 min readCopy differs from a move. It also assumes you have seen impl Add for Vec2 from the earlier chapters. Unary operators follow the same pattern as binary ones, with one operand and no Rhs parame5 min readimpl Add for T, know what the Output associated type is, and understand moves and Copy. It covers what to do when the operand type is expensive to move or clone.3 min readAdd and its Output type in earlier chapters. The new ideas here are that indexing returns a reference, that one expression can call two differen4 min readderive works, and how Option and match work. It also assumes you have seen the std::ops traits such as Add. Comparison operators look like the arithmetic ones, but they differ in three ways: they live in `std::cm5 min readAdd trait, its Output associated type, and how operators move their operands. You will use those pieces to write functions that work for i32, f64, and your own types.5 min readimpl Trait for Type blocks and use generic parameters and associated types. It also assumes you have read the earlier chapters on Add, Output and the Rhs parameter. Here you will learn which operator impls the compiler accepts, why it rejects the others, an5 min read&, |, ^, !, << and >>. It assumes you can already implement Add for a struct, know what the Output associated type is, and are comfortable with Copy, generics and trait bounds. The bitwise traits work like Add and AddAssign. This c5 min read+, -, * and / on values and references needs many near-identical blocks. This chapter shows how to generate them with declarative macros (macro_rules!).4 min readimpl blocks for traits and know the std::ops traits from earlier in this tutorial: Add, AddAssign, Neg, Index, and the Output associated type. It also assumes you know how Copy and moves affect operands. The chapter has no new syntax. It covers the 5 min read