Add an example to Serialize

This commit is contained in:
David Tolnay 2017-04-13 13:55:51 -07:00
parent 9253fccf04
commit 7d16710fb4
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82

View File

@ -200,6 +200,29 @@ pub trait Serialize {
/// See the [Implementing `Serialize`] section of the manual for more
/// information about how to implement this method.
///
/// ```rust
/// use serde::ser::{Serialize, Serializer, SerializeStruct};
///
/// struct Person {
/// name: String,
/// age: u8,
/// phones: Vec<String>,
/// }
///
/// // This is what #[derive(Serialize)] would generate.
/// impl Serialize for Person {
/// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
/// where S: Serializer
/// {
/// let mut s = serializer.serialize_struct("Person", 3)?;
/// s.serialize_field("name", &self.name)?;
/// s.serialize_field("age", &self.age)?;
/// s.serialize_field("phones", &self.phones)?;
/// s.end()
/// }
/// }
/// ```
///
/// [Implementing `Serialize`]: https://serde.rs/impl-serialize.html
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where