I am creating a programming language for fun in Rust. Currently, I am working on an error function that underlines the given code and throws an error code.
fn error(message: &str, code: &str, mut line: Vec<String>, index: i32, file: &str){
println!("{}{}{}: {}", "error[".red(), code.red(), "]".red(), message);
println!("> in {}\n|", file);
let mut whole = "".to_string();
for i in line.clone() {
whole.push_str(i.as_str());
}
println!("| {}...", whole.cyan());
let mut spaces = "".to_string();
line.pop();
let mut j = 0;
'spaces: while j < line.clone().len(){
match line.get(j){
Some(k) => {
for l in k.chars() {
spaces.push_str(" ");
}
},
None => break 'spaces
}
j = j + 1
}
print!("| {}^", spaces);
let mut arr = vec![0; line[index as usize].len()];
for l in arr {
print!("~")
}
println!("")
}
Which I am calling as error("explicitly defined private", "f1", line.clone(), index). Line is ["def", " ", "private", " ", "public"], index is 4, and file is examples/functions.surf.
This function should output
error[f1]: explicitly defined private
> in examples/functions.surf
|
| def private public...
| ^~~~~~
But instead is panicking
error[f1]: explicitly defined private
> in examples/functions.surf
|
| def private public...
thread 'main' panicked at 'index out of bounds: the len is 4 but the index is 4', /Users/travis/build/rust-lang/rust/src/libcollections/vec.rs:1552
stack backtrace:
0: std::sys::imp::backtrace::tracing::imp::unwind_backtrace
1: std::panicking::default_hook::{{closure}}
2: std::panicking::default_hook
3: std::panicking::rust_panic_with_hook
4: std::panicking::begin_panic
5: std::panicking::begin_panic_fmt
6: rust_begin_unwind
7: core::panicking::panic_fmt
8: core::panicking::panic_bounds_check
9: <collections::vec::Vec<T> as core::ops::Index<usize>>::index
10: surf::interpreter::error
11: surf::interpreter::interpret
12: surf::main
13: __rust_maybe_catch_panic
14: std::rt::lang_start
15: main
| ^
Why is this so? I think every loop is written in a way the index shouldn't go out of bounds.
(I am running cargo build with Rust 1.19.0)