Pure Programmer
Blue Matrix


Cluster Map

Project: Base Conversion

Write a program that converts an integer to any base between 2 and 16. The program should accept two arguments, the maximum number to convert and the base of conversion. The program will then print the numbers 0 to the maximum each converted to the base supplied. The conversion should take place in a separate function that takes the number to convert and the base. Be sure to test the preconditions in the conversion function with assertions.

Output
$ rustc BaseConversion.rs error: unknown format trait `s` --> BaseConversion.rs:41:38 | 41 | println!("{}", format!("Syntax: {0:s} max base", utils::program_name().expect("program name should not be empty!"))); | ^ | = 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 `d` --> BaseConversion.rs:50:31 | 50 | println!("{}", format!("{0:d} = {2:s}{1:s}", i, composeSubscript(base), convertToBase(i, base))); | ^ | = 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 `s` --> BaseConversion.rs:50:39 | 50 | println!("{}", format!("{0:d} = {2:s}{1:s}", i, composeSubscript(base), convertToBase(i, base))); | ^ | = 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 `s` --> BaseConversion.rs:50:44 | 50 | println!("{}", format!("{0:d} = {2:s}{1:s}", i, composeSubscript(base), convertToBase(i, base))); | ^ | = 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 `args` in this scope --> BaseConversion.rs:40:5 | 40 | if args.len() != 3 { | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find function `exit` in this scope --> BaseConversion.rs:42:3 | 42 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::process::exit; | error[E0425]: cannot find value `Utils` in this scope --> BaseConversion.rs:44:18 | 44 | let max:isize = Utils.stoiWithDefault(args[1], 64); | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> BaseConversion.rs:44:40 | 44 | let max:isize = Utils.stoiWithDefault(args[1], 64); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find value `Utils` in this scope --> BaseConversion.rs:45:19 | 45 | let base:isize = Utils.stoiWithDefault(args[2], 16); | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> BaseConversion.rs:45:41 | 45 | let base:isize = Utils.stoiWithDefault(args[2], 16); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0308]: mismatched types --> BaseConversion.rs:15:10 | 11 | fn convertToBase(i:isize, base:isize) -> String { | ------ expected `String` because of return type ... 15 | return "0"; | ^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` error[E0277]: the type `[&'static str]` cannot be indexed by `isize` --> BaseConversion.rs:20:35 | 20 | result = &(String::from(symbols[digit]) + result); | ^^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[&'static str]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<&'static str>` to implement `Index<isize>` error[E0308]: mismatched types --> BaseConversion.rs:33:55 | 33 | result = &(String::from(char::from_u32(subscript0 + (x % 10))) + result); | ^^^^^^^^ expected `i32`, found `isize` error[E0277]: cannot add `isize` to `i32` --> BaseConversion.rs:33:53 | 33 | result = &(String::from(char::from_u32(subscript0 + (x % 10))) + result); | ^ no implementation for `i32 + isize` | = help: the trait `Add<isize>` is not implemented for `i32` = help: the following other types implement trait `Add<Rhs>`: <&'a i32 as Add<i32>> <&i32 as Add<&i32>> <i32 as Add<&i32>> <i32 as Add> error[E0308]: mismatched types --> BaseConversion.rs:33:42 | 33 | result = &(String::from(char::from_u32(subscript0 + (x % 10))) + result); | -------------- ^^^^^^^^^^^^^^^^^^^^^ expected `u32`, found `i32` | | | arguments to this function are incorrect | note: associated function defined here --> /opt/local/libexec/rust/src/rustc-1.71.1-src/library/core/src/char/methods.rs:139:18 help: you can convert an `i32` to a `u32` and panic if the converted value doesn't fit | 33 | result = &(String::from(char::from_u32((subscript0 + (x % 10)).try_into().unwrap())) + result); | + +++++++++++++++++++++ error[E0277]: the trait bound `String: From<Option<char>>` is not satisfied --> BaseConversion.rs:33:27 | 33 | result = &(String::from(char::from_u32(subscript0 + (x % 10))) + result); | ------------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<Option<char>>` is not implemented for `String` | | | required by a bound introduced by this call | = help: the following other types implement trait `From<T>`: <String as From<&String>> <String as From<&mut str>> <String as From<&str>> <String as From<Box<str>>> <String as From<Cow<'a, str>>> <String as From<char>> error[E0308]: mismatched types --> BaseConversion.rs:36:9 | 28 | fn composeSubscript(base:isize) -> String { | ------ expected `String` because of return type ... 36 | return result; | ^^^^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` error: aborting due to 17 previous errors Some errors have detailed explanations: E0277, E0308, E0425. For more information about an error, try `rustc --explain E0277`. $ rustc BaseConversion.rs error: unknown format trait `s` --> BaseConversion.rs:41:38 | 41 | println!("{}", format!("Syntax: {0:s} max base", utils::program_name().expect("program name should not be empty!"))); | ^ | = 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 `d` --> BaseConversion.rs:50:31 | 50 | println!("{}", format!("{0:d} = {2:s}{1:s}", i, composeSubscript(base), convertToBase(i, base))); | ^ | = 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 `s` --> BaseConversion.rs:50:39 | 50 | println!("{}", format!("{0:d} = {2:s}{1:s}", i, composeSubscript(base), convertToBase(i, base))); | ^ | = 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 `s` --> BaseConversion.rs:50:44 | 50 | println!("{}", format!("{0:d} = {2:s}{1:s}", i, composeSubscript(base), convertToBase(i, base))); | ^ | = 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 `args` in this scope --> BaseConversion.rs:40:5 | 40 | if args.len() != 3 { | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find function `exit` in this scope --> BaseConversion.rs:42:3 | 42 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::process::exit; | error[E0425]: cannot find value `Utils` in this scope --> BaseConversion.rs:44:18 | 44 | let max:isize = Utils.stoiWithDefault(args[1], 64); | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> BaseConversion.rs:44:40 | 44 | let max:isize = Utils.stoiWithDefault(args[1], 64); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find value `Utils` in this scope --> BaseConversion.rs:45:19 | 45 | let base:isize = Utils.stoiWithDefault(args[2], 16); | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> BaseConversion.rs:45:41 | 45 | let base:isize = Utils.stoiWithDefault(args[2], 16); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0308]: mismatched types --> BaseConversion.rs:15:10 | 11 | fn convertToBase(i:isize, base:isize) -> String { | ------ expected `String` because of return type ... 15 | return "0"; | ^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` error[E0277]: the type `[&'static str]` cannot be indexed by `isize` --> BaseConversion.rs:20:35 | 20 | result = &(String::from(symbols[digit]) + result); | ^^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[&'static str]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<&'static str>` to implement `Index<isize>` error[E0308]: mismatched types --> BaseConversion.rs:33:55 | 33 | result = &(String::from(char::from_u32(subscript0 + (x % 10))) + result); | ^^^^^^^^ expected `i32`, found `isize` error[E0277]: cannot add `isize` to `i32` --> BaseConversion.rs:33:53 | 33 | result = &(String::from(char::from_u32(subscript0 + (x % 10))) + result); | ^ no implementation for `i32 + isize` | = help: the trait `Add<isize>` is not implemented for `i32` = help: the following other types implement trait `Add<Rhs>`: <&'a i32 as Add<i32>> <&i32 as Add<&i32>> <i32 as Add<&i32>> <i32 as Add> error[E0308]: mismatched types --> BaseConversion.rs:33:42 | 33 | result = &(String::from(char::from_u32(subscript0 + (x % 10))) + result); | -------------- ^^^^^^^^^^^^^^^^^^^^^ expected `u32`, found `i32` | | | arguments to this function are incorrect | note: associated function defined here --> /opt/local/libexec/rust/src/rustc-1.71.1-src/library/core/src/char/methods.rs:139:18 help: you can convert an `i32` to a `u32` and panic if the converted value doesn't fit | 33 | result = &(String::from(char::from_u32((subscript0 + (x % 10)).try_into().unwrap())) + result); | + +++++++++++++++++++++ error[E0277]: the trait bound `String: From<Option<char>>` is not satisfied --> BaseConversion.rs:33:27 | 33 | result = &(String::from(char::from_u32(subscript0 + (x % 10))) + result); | ------------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<Option<char>>` is not implemented for `String` | | | required by a bound introduced by this call | = help: the following other types implement trait `From<T>`: <String as From<&String>> <String as From<&mut str>> <String as From<&str>> <String as From<Box<str>>> <String as From<Cow<'a, str>>> <String as From<char>> error[E0308]: mismatched types --> BaseConversion.rs:36:9 | 28 | fn composeSubscript(base:isize) -> String { | ------ expected `String` because of return type ... 36 | return result; | ^^^^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` error: aborting due to 17 previous errors Some errors have detailed explanations: E0277, E0308, E0425. For more information about an error, try `rustc --explain E0277`. $ rustc BaseConversion.rs error: unknown format trait `s` --> BaseConversion.rs:41:38 | 41 | println!("{}", format!("Syntax: {0:s} max base", utils::program_name().expect("program name should not be empty!"))); | ^ | = 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 `d` --> BaseConversion.rs:50:31 | 50 | println!("{}", format!("{0:d} = {2:s}{1:s}", i, composeSubscript(base), convertToBase(i, base))); | ^ | = 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 `s` --> BaseConversion.rs:50:39 | 50 | println!("{}", format!("{0:d} = {2:s}{1:s}", i, composeSubscript(base), convertToBase(i, base))); | ^ | = 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 `s` --> BaseConversion.rs:50:44 | 50 | println!("{}", format!("{0:d} = {2:s}{1:s}", i, composeSubscript(base), convertToBase(i, base))); | ^ | = 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 `args` in this scope --> BaseConversion.rs:40:5 | 40 | if args.len() != 3 { | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find function `exit` in this scope --> BaseConversion.rs:42:3 | 42 | exit(1); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::process::exit; | error[E0425]: cannot find value `Utils` in this scope --> BaseConversion.rs:44:18 | 44 | let max:isize = Utils.stoiWithDefault(args[1], 64); | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> BaseConversion.rs:44:40 | 44 | let max:isize = Utils.stoiWithDefault(args[1], 64); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0425]: cannot find value `Utils` in this scope --> BaseConversion.rs:45:19 | 45 | let base:isize = Utils.stoiWithDefault(args[2], 16); | ^^^^^ not found in this scope error[E0425]: cannot find value `args` in this scope --> BaseConversion.rs:45:41 | 45 | let base:isize = Utils.stoiWithDefault(args[2], 16); | ^^^^ not found in this scope | help: consider importing this function | 7 + use std::env::args; | error[E0308]: mismatched types --> BaseConversion.rs:15:10 | 11 | fn convertToBase(i:isize, base:isize) -> String { | ------ expected `String` because of return type ... 15 | return "0"; | ^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` error[E0277]: the type `[&'static str]` cannot be indexed by `isize` --> BaseConversion.rs:20:35 | 20 | result = &(String::from(symbols[digit]) + result); | ^^^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[&'static str]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<&'static str>` to implement `Index<isize>` error[E0308]: mismatched types --> BaseConversion.rs:33:55 | 33 | result = &(String::from(char::from_u32(subscript0 + (x % 10))) + result); | ^^^^^^^^ expected `i32`, found `isize` error[E0277]: cannot add `isize` to `i32` --> BaseConversion.rs:33:53 | 33 | result = &(String::from(char::from_u32(subscript0 + (x % 10))) + result); | ^ no implementation for `i32 + isize` | = help: the trait `Add<isize>` is not implemented for `i32` = help: the following other types implement trait `Add<Rhs>`: <&'a i32 as Add<i32>> <&i32 as Add<&i32>> <i32 as Add<&i32>> <i32 as Add> error[E0308]: mismatched types --> BaseConversion.rs:33:42 | 33 | result = &(String::from(char::from_u32(subscript0 + (x % 10))) + result); | -------------- ^^^^^^^^^^^^^^^^^^^^^ expected `u32`, found `i32` | | | arguments to this function are incorrect | note: associated function defined here --> /opt/local/libexec/rust/src/rustc-1.71.1-src/library/core/src/char/methods.rs:139:18 help: you can convert an `i32` to a `u32` and panic if the converted value doesn't fit | 33 | result = &(String::from(char::from_u32((subscript0 + (x % 10)).try_into().unwrap())) + result); | + +++++++++++++++++++++ error[E0277]: the trait bound `String: From<Option<char>>` is not satisfied --> BaseConversion.rs:33:27 | 33 | result = &(String::from(char::from_u32(subscript0 + (x % 10))) + result); | ------------ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `From<Option<char>>` is not implemented for `String` | | | required by a bound introduced by this call | = help: the following other types implement trait `From<T>`: <String as From<&String>> <String as From<&mut str>> <String as From<&str>> <String as From<Box<str>>> <String as From<Cow<'a, str>>> <String as From<char>> error[E0308]: mismatched types --> BaseConversion.rs:36:9 | 28 | fn composeSubscript(base:isize) -> String { | ------ expected `String` because of return type ... 36 | return result; | ^^^^^^- help: try using a conversion method: `.to_string()` | | | expected `String`, found `&str` error: aborting due to 17 previous errors Some errors have detailed explanations: E0277, E0308, E0425. For more information about an error, try `rustc --explain E0277`.

Solution