Lists

This page is under construction. Please come back later.
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
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!("{0}^2 = {1}", i, squares[i]);
i += 1;
}
}
println!("{}", 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`.
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
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!("{0}^2 = {1}", 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`.
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
fn main() {
let mut names:Vec<String> = vec!["Fred", "Wilma", "Barney", "Betty"];
for name in names.iter() {
println!("Hello, {}!", name);
}
names = vec!["Harry", "Ron", "Hermione"];
for name in names.iter() {
println!("Hello, {}!", name);
}
}
Output
$ rustc Lists3.rs
$ ./Lists3
Hello, Fred!
Hello, Wilma!
Hello, Barney!
Hello, Betty!
Hello, Harry!
Hello, Ron!
Hello, Hermione!
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#[macro_use]
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`.
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#[macro_use]
mod utils;
use std::env;
fn main() {
let args: Vec<String> = env::args().collect();
let mut names:Vec<String> = 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 = args[i].trim().parse::<i32>().unwrap_or(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
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
- Array Last Element
- Central Limit Theorem
- Chromatic Scale Frequencies
- Income Tax (Revisited)
- Morse Code
- Renard Numbers (Preferred Numbers)
- Sample Mean and Standard Deviation
- Sieve of Eratosthenes
References
- [[Rust Language Reference]]
- [[Rust Compiler]]
Pure Programmer


