Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add two doc examples #211

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/array_string.rs
Expand Up @@ -30,6 +30,24 @@ use serde::{Serialize, Deserialize, Serializer, Deserializer};
///
/// The string is a contiguous value that you can store directly on the stack
/// if needed.
///
/// ## Example
/// ```rust
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use arrayvec::ArrayString;
///
/// // Create a new ArrayString with a capacity of 12
/// let mut hello = ArrayString::<12>::from("Hello")?;
///
/// let mut hello_world = hello.clone();
/// hello_world.push_str(" world!");
/// assert_eq!(hello_world.as_str(), "Hello world!");
///
/// // Too long!
/// assert!(hello.try_push_str(" to the world and the universe!").is_err());
/// # Ok(())
/// # }
/// ```
#[derive(Copy)]
pub struct ArrayString<const CAP: usize> {
// the `len` first elements of the array are initialized
Expand Down
14 changes: 14 additions & 0 deletions src/lib.rs
Expand Up @@ -11,6 +11,20 @@
//! - Optional
//! - Enable serialization for ArrayVec and ArrayString using serde 1.x
//!
//! ## Example
//! ```rust
//! use arrayvec::{ArrayVec, CapacityError};
//!
//! // Creates a new ArrayVec with a capacity of 3 and contents [1, 2, 3].
//! let mut stack = ArrayVec::from([1, 2, 3]);
//! assert_eq!(stack.pop(), Some(3));
//! stack.push(4);
//!
//! // Now the stack is full:
//! assert!(stack.is_full());
//! assert_eq!(stack.try_push(5), Err(CapacityError::new(5)))
//! ```
//!
//! ## Rust Version
//!
//! This version of arrayvec requires Rust 1.51 or later.
Expand Down