Pure Programmer
Blue Matrix


Cluster Map

Lists

L1

This page is under construction. Please come back later.

Lists1.rs
mod utils;

fn main() {
	let NUM_SQUARES:isize = 5;
	let mut squares:Vec<isize> = Vec::new();
// Put the squares into the list
	{
		let mut i:isize = 0;
		while i < NUM_SQUARES {
			squares.push(i * i);
			i += 1;
		}
	}
// Print out the squares from the list
	{
		let mut i:isize = 0;
		while i < squares.len() {
			println!("{}", format!("{0:d}^2 = {1:d}", i, squares[i]));
			i += 1;
		}
	}

	println!("{}", utils::list_to_string(squares));
}

Output
$ rustc Lists1.rs error: unknown format trait `d` --> Lists1.rs:18:31 | 18 | println!("{}", format!("{0:d}^2 = {1:d}", i, squares[i])); | ^ | = 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` --> Lists1.rs:18:41 | 18 | println!("{}", format!("{0:d}^2 = {1:d}", i, squares[i])); | ^ | = 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[E0308]: mismatched types --> Lists1.rs:17:13 | 17 | while i < squares.len() { | - ^^^^^^^^^^^^^ expected `isize`, found `usize` | | | expected because this is `isize` | help: you can convert a `usize` to an `isize` and panic if the converted value doesn't fit | 17 | while i < squares.len().try_into().unwrap() { | ++++++++++++++++++++ error[E0277]: the type `[isize]` cannot be indexed by `isize` --> Lists1.rs:18:57 | 18 | println!("{}", format!("{0:d}^2 = {1:d}", i, squares[i])); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error: aborting due to 4 previous errors Some errors have detailed explanations: E0277, E0308. For more information about an error, try `rustc --explain E0277`.
Lists2.rs
fn main() {
	let mut squares:Vec<isize> = vec![0, 1, 4, 9, 16, 25];

// Print out the squares from the list
	{
		let mut i:isize = 0;
		while i < squares.len() {
			println!("{}", format!("{0:d}^2 = {1:d}", i, squares[i]));
			i += 1;
		}
	}
}

Output
$ rustc Lists2.rs error: unknown format trait `d` --> Lists2.rs:8:31 | 8 | println!("{}", format!("{0:d}^2 = {1:d}", i, squares[i])); | ^ | = 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` --> Lists2.rs:8:41 | 8 | println!("{}", format!("{0:d}^2 = {1:d}", i, squares[i])); | ^ | = 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[E0308]: mismatched types --> Lists2.rs:7:13 | 7 | while i < squares.len() { | - ^^^^^^^^^^^^^ expected `isize`, found `usize` | | | expected because this is `isize` | help: you can convert a `usize` to an `isize` and panic if the converted value doesn't fit | 7 | while i < squares.len().try_into().unwrap() { | ++++++++++++++++++++ error[E0277]: the type `[isize]` cannot be indexed by `isize` --> Lists2.rs:8:57 | 8 | println!("{}", format!("{0:d}^2 = {1:d}", i, squares[i])); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error: aborting due to 4 previous errors Some errors have detailed explanations: E0277, E0308. For more information about an error, try `rustc --explain E0277`.
Lists3.rs
fn main() {
	let mut names:Vec<&str> = vec!["Fred", "Wilma", "Barney", "Betty"];

	for name in names {
		println!("Hello, {}!", name);
	}

	names = vec!["Harry", "Ron", "Hermione"];
	for name in names {
		println!("Hello, {}!", name);
	}
}

Output
$ rustc Lists3.rs $ ./Lists3 Hello, Fred! Hello, Wilma! Hello, Barney! Hello, Betty! Hello, Harry! Hello, Ron! Hello, Hermione!
Lists4.rs
mod utils;
use std::env;

fn main() {
	let args: Vec<String> = env::args().collect();
// Print out the command line arguments
	{
		let mut i:isize = 1;
		while i <= (args.len() - 1) {
			println!("{}:{}", i, args[i]);
			i += 1;
		}
	}
}

Output
$ rustc Lists4.rs error[E0308]: mismatched types --> Lists4.rs:9:14 | 9 | while i <= (args.len() - 1) { | - ^^^^^^^^^^^^^^^^ expected `isize`, found `usize` | | | expected because this is `isize` | help: you can convert a `usize` to an `isize` and panic if the converted value doesn't fit | 9 | while i <= ((args.len() - 1)).try_into().unwrap() { | + +++++++++++++++++++++ error[E0277]: the type `[String]` cannot be indexed by `isize` --> Lists4.rs:10:30 | 10 | println!("{}:{}", i, args[i]); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[String]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<String>` to implement `Index<isize>` error: aborting due to 2 previous errors Some errors have detailed explanations: E0277, E0308. For more information about an error, try `rustc --explain E0277`. $ rustc Lists4.rs error[E0308]: mismatched types --> Lists4.rs:9:14 | 9 | while i <= (args.len() - 1) { | - ^^^^^^^^^^^^^^^^ expected `isize`, found `usize` | | | expected because this is `isize` | help: you can convert a `usize` to an `isize` and panic if the converted value doesn't fit | 9 | while i <= ((args.len() - 1)).try_into().unwrap() { | + +++++++++++++++++++++ error[E0277]: the type `[String]` cannot be indexed by `isize` --> Lists4.rs:10:30 | 10 | println!("{}:{}", i, args[i]); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[String]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<String>` to implement `Index<isize>` error: aborting due to 2 previous errors Some errors have detailed explanations: E0277, E0308. For more information about an error, try `rustc --explain E0277`.
Lists5.rs
mod utils;
use std::env;

fn main() {
	let args: Vec<String> = env::args().collect();
	let mut names:Vec<&str> = vec!["Fred", "Wilma", "Barney", "Betty"];

// Print out the name based on command line argument 1-4
	{
		let mut i:isize = 1;
		while i <= (args.len() - 1) {
			let x:isize = Utils.stoiWithDefault(args[i], 0);
			println!("{}:{}", i, names[x - 1]);
			i += 1;
		}
	}
}

Output
$ rustc Lists5.rs error[E0425]: cannot find value `Utils` in this scope --> Lists5.rs:12:18 | 12 | let x:isize = Utils.stoiWithDefault(args[i], 0); | ^^^^^ not found in this scope error[E0308]: mismatched types --> Lists5.rs:11:14 | 11 | while i <= (args.len() - 1) { | - ^^^^^^^^^^^^^^^^ expected `isize`, found `usize` | | | expected because this is `isize` | help: you can convert a `usize` to an `isize` and panic if the converted value doesn't fit | 11 | while i <= ((args.len() - 1)).try_into().unwrap() { | + +++++++++++++++++++++ error[E0277]: the type `[String]` cannot be indexed by `isize` --> Lists5.rs:12:45 | 12 | let x:isize = Utils.stoiWithDefault(args[i], 0); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[String]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<String>` to implement `Index<isize>` error: aborting due to 3 previous errors Some errors have detailed explanations: E0277, E0308, E0425. For more information about an error, try `rustc --explain E0277`. $ rustc Lists5.rs error[E0425]: cannot find value `Utils` in this scope --> Lists5.rs:12:18 | 12 | let x:isize = Utils.stoiWithDefault(args[i], 0); | ^^^^^ not found in this scope error[E0308]: mismatched types --> Lists5.rs:11:14 | 11 | while i <= (args.len() - 1) { | - ^^^^^^^^^^^^^^^^ expected `isize`, found `usize` | | | expected because this is `isize` | help: you can convert a `usize` to an `isize` and panic if the converted value doesn't fit | 11 | while i <= ((args.len() - 1)).try_into().unwrap() { | + +++++++++++++++++++++ error[E0277]: the type `[String]` cannot be indexed by `isize` --> Lists5.rs:12:45 | 12 | let x:isize = Utils.stoiWithDefault(args[i], 0); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[String]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<String>` to implement `Index<isize>` error: aborting due to 3 previous errors Some errors have detailed explanations: E0277, E0308, E0425. For more information about an error, try `rustc --explain E0277`. $ rustc Lists5.rs error[E0425]: cannot find value `Utils` in this scope --> Lists5.rs:12:18 | 12 | let x:isize = Utils.stoiWithDefault(args[i], 0); | ^^^^^ not found in this scope error[E0308]: mismatched types --> Lists5.rs:11:14 | 11 | while i <= (args.len() - 1) { | - ^^^^^^^^^^^^^^^^ expected `isize`, found `usize` | | | expected because this is `isize` | help: you can convert a `usize` to an `isize` and panic if the converted value doesn't fit | 11 | while i <= ((args.len() - 1)).try_into().unwrap() { | + +++++++++++++++++++++ error[E0277]: the type `[String]` cannot be indexed by `isize` --> Lists5.rs:12:45 | 12 | let x:isize = Utils.stoiWithDefault(args[i], 0); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[String]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<String>` to implement `Index<isize>` error: aborting due to 3 previous errors Some errors have detailed explanations: E0277, E0308, E0425. For more information about an error, try `rustc --explain E0277`.

Questions

Projects

More ★'s indicate higher difficulty level.

References