Pure Programmer
Blue Matrix


Cluster Map

Project: Aligning Floating Point with format()

Write a program that demonstrates floating point formatting using default (most compact), fixed point and exponential (scientific) formats using format() style output. Print using no precision, precision of 2 and a field width of 10 with precision 2. Your program should reproduce the output shown using the following variables:

  • π = 3.141592653589793
  • 𝑒 = 2.718281828459045
  • avogadro = 6.0221412927e23
  • electron_mass = 9.1093821545e-31

Output
$ rustc AligningFloatingPointWithFormat.rs error: unknown format trait `g` --> AligningFloatingPointWithFormat.rs:14:32 | 14 | println!("{}", format!("π: {0:g} {0:.2g} |{0:10.2g}|", PI)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `g` --> AligningFloatingPointWithFormat.rs:14:40 | 14 | println!("{}", format!("π: {0:g} {0:.2g} |{0:10.2g}|", PI)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `g` --> AligningFloatingPointWithFormat.rs:14:51 | 14 | println!("{}", format!("π: {0:g} {0:.2g} |{0:10.2g}|", PI)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `g` --> AligningFloatingPointWithFormat.rs:15:32 | 15 | println!("{}", format!("ℯ: {0:g} {0:.2g} |{0:10.2g}|", E)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `g` --> AligningFloatingPointWithFormat.rs:15:40 | 15 | println!("{}", format!("ℯ: {0:g} {0:.2g} |{0:10.2g}|", E)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `g` --> AligningFloatingPointWithFormat.rs:15:51 | 15 | println!("{}", format!("ℯ: {0:g} {0:.2g} |{0:10.2g}|", E)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `g` --> AligningFloatingPointWithFormat.rs:16:40 | 16 | println!("{}", format!("N\u{2090}: {0:g} {0:.2g} |{0:10.2g}|", avogadro)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `g` --> AligningFloatingPointWithFormat.rs:16:48 | 16 | println!("{}", format!("N\u{2090}: {0:g} {0:.2g} |{0:10.2g}|", avogadro)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `g` --> AligningFloatingPointWithFormat.rs:16:59 | 16 | println!("{}", format!("N\u{2090}: {0:g} {0:.2g} |{0:10.2g}|", avogadro)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `g` --> AligningFloatingPointWithFormat.rs:17:40 | 17 | println!("{}", format!("m\u{2091}: {0:g} {0:.2g} |{0:10.2g}|", electron_mass)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `g` --> AligningFloatingPointWithFormat.rs:17:48 | 17 | println!("{}", format!("m\u{2091}: {0:g} {0:.2g} |{0:10.2g}|", electron_mass)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `g` --> AligningFloatingPointWithFormat.rs:17:59 | 17 | println!("{}", format!("m\u{2091}: {0:g} {0:.2g} |{0:10.2g}|", electron_mass)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> AligningFloatingPointWithFormat.rs:20:32 | 20 | println!("{}", format!("π: {0:f} {0:.2f} |{0:10.2f}|", PI)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> AligningFloatingPointWithFormat.rs:20:40 | 20 | println!("{}", format!("π: {0:f} {0:.2f} |{0:10.2f}|", PI)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> AligningFloatingPointWithFormat.rs:20:51 | 20 | println!("{}", format!("π: {0:f} {0:.2f} |{0:10.2f}|", PI)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> AligningFloatingPointWithFormat.rs:21:32 | 21 | println!("{}", format!("ℯ: {0:f} {0:.2f} |{0:10.2f}|", E)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> AligningFloatingPointWithFormat.rs:21:40 | 21 | println!("{}", format!("ℯ: {0:f} {0:.2f} |{0:10.2f}|", E)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> AligningFloatingPointWithFormat.rs:21:51 | 21 | println!("{}", format!("ℯ: {0:f} {0:.2f} |{0:10.2f}|", E)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> AligningFloatingPointWithFormat.rs:22:40 | 22 | println!("{}", format!("N\u{2090}: {0:f} {0:.2f} |{0:10.2f}|", avogadro)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> AligningFloatingPointWithFormat.rs:22:48 | 22 | println!("{}", format!("N\u{2090}: {0:f} {0:.2f} |{0:10.2f}|", avogadro)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> AligningFloatingPointWithFormat.rs:22:59 | 22 | println!("{}", format!("N\u{2090}: {0:f} {0:.2f} |{0:10.2f}|", avogadro)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> AligningFloatingPointWithFormat.rs:23:40 | 23 | println!("{}", format!("m\u{2091}: {0:f} {0:.2f} |{0:10.2f}|", electron_mass)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> AligningFloatingPointWithFormat.rs:23:48 | 23 | println!("{}", format!("m\u{2091}: {0:f} {0:.2f} |{0:10.2f}|", electron_mass)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error: unknown format trait `f` --> AligningFloatingPointWithFormat.rs:23:59 | 23 | println!("{}", format!("m\u{2091}: {0:f} {0:.2f} |{0:10.2f}|", electron_mass)); | ^ | = note: the only appropriate formatting traits are: - ``, which uses the `Display` trait - `?`, which uses the `Debug` trait - `e`, which uses the `LowerExp` trait - `E`, which uses the `UpperExp` trait - `o`, which uses the `Octal` trait - `p`, which uses the `Pointer` trait - `b`, which uses the `Binary` trait - `x`, which uses the `LowerHex` trait - `X`, which uses the `UpperHex` trait error[E0425]: cannot find value `PI` in this scope --> AligningFloatingPointWithFormat.rs:14:57 | 14 | println!("{}", format!("π: {0:g} {0:.2g} |{0:10.2g}|", PI)); | ^^ not found in this scope | help: consider importing one of these items | 7 + use consts::PI; | 7 + use std::f32::consts::PI; | error[E0425]: cannot find value `E` in this scope --> AligningFloatingPointWithFormat.rs:15:57 | 15 | println!("{}", format!("ℯ: {0:g} {0:.2g} |{0:10.2g}|", E)); | ^ not found in this scope | help: consider importing one of these items | 7 + use consts::E; | 7 + use std::f32::consts::E; | error[E0425]: cannot find value `PI` in this scope --> AligningFloatingPointWithFormat.rs:20:57 | 20 | println!("{}", format!("π: {0:f} {0:.2f} |{0:10.2f}|", PI)); | ^^ not found in this scope | help: consider importing one of these items | 7 + use consts::PI; | 7 + use std::f32::consts::PI; | error[E0425]: cannot find value `E` in this scope --> AligningFloatingPointWithFormat.rs:21:57 | 21 | println!("{}", format!("ℯ: {0:f} {0:.2f} |{0:10.2f}|", E)); | ^ not found in this scope | help: consider importing one of these items | 7 + use consts::E; | 7 + use std::f32::consts::E; | error[E0425]: cannot find value `PI` in this scope --> AligningFloatingPointWithFormat.rs:26:57 | 26 | println!("{}", format!("π: {0:e} {0:.2e} |{0:10.2e}|", PI)); | ^^ not found in this scope | help: consider importing one of these items | 7 + use consts::PI; | 7 + use std::f32::consts::PI; | error[E0425]: cannot find value `E` in this scope --> AligningFloatingPointWithFormat.rs:27:57 | 27 | println!("{}", format!("ℯ: {0:e} {0:.2e} |{0:10.2e}|", E)); | ^ not found in this scope | help: consider importing one of these items | 7 + use consts::E; | 7 + use std::f32::consts::E; | warning: unused import: `std::f64::consts` --> AligningFloatingPointWithFormat.rs:7:5 | 7 | use std::f64::consts; | ^^^^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default error: aborting due to 30 previous errors; 1 warning emitted For more information about this error, try `rustc --explain E0425`.

Solution