Add an example to Serialize
This commit is contained in:
parent
9253fccf04
commit
7d16710fb4
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user