|
| 1 | +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +//! Formatting support for `String`. |
| 12 | +//! |
| 13 | +//! See `core::fmt` and `std::fmt` for full documentation on string |
| 14 | +//! formatting. |
| 15 | +
|
| 16 | +#![stable(feature = "rust1", since = "1.0.0")] |
| 17 | + |
| 18 | +use core::fmt; |
| 19 | + |
| 20 | +use string; |
| 21 | + |
| 22 | +/// The format function takes a precompiled format string and a list of |
| 23 | +/// arguments, to return the resulting formatted string. |
| 24 | +/// |
| 25 | +/// # Arguments |
| 26 | +/// |
| 27 | +/// * args - a structure of arguments generated via the `format_args!` macro. |
| 28 | +/// |
| 29 | +/// # Example |
| 30 | +/// |
| 31 | +/// ```rust |
| 32 | +/// use std::fmt; |
| 33 | +/// |
| 34 | +/// let s = fmt::format(format_args!("Hello, {}!", "world")); |
| 35 | +/// assert_eq!(s, "Hello, world!".to_string()); |
| 36 | +/// ``` |
| 37 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 38 | +pub fn format(args: fmt::Arguments) -> string::String { |
| 39 | + // FIXME #21826 |
| 40 | + use core::fmt::Writer; |
| 41 | + let mut output = string::String::new(); |
| 42 | + let _ = write!(&mut output, "{}", args); |
| 43 | + output |
| 44 | +} |
| 45 | + |
| 46 | +#[cfg(test)] |
| 47 | +mod tests { |
| 48 | + use prelude::*; |
| 49 | + use fmt; |
| 50 | + |
| 51 | + #[test] |
| 52 | + fn test_format() { |
| 53 | + let s = fmt::format(format_args!("Hello, {}!", "world")); |
| 54 | + assert_eq!(s.as_slice(), "Hello, world!"); |
| 55 | + } |
| 56 | +} |
0 commit comments