Whenever I try to put collect_input as a &str for configurable_data.name, this error message appears:
error[E0599]: no method named `as_str` found for unit type `()` in the current scope
--> src/main.rs:72:80
|
72 | ... configurable_data.name = collect_input.as_str();
| ^^^^^^ method not found in `()`
For more information about this error, try `rustc --explain E0599`.
error: could not compile `test_move` (bin "test_move") due to 1 previous error
And if I use
format("{:?}" collect_input.to_string()).as_str();
Give error E716
The entire code:
use crossterm::cursor::position;
use crossterm::cursor::MoveTo;
use crossterm::event::{poll, read, Event, KeyCode};
use crossterm::execute;
use crossterm::terminal;
use crossterm::QueueableCommand;
use std::io::{stderr, stdout, Write};
use std::time::Duration;
fn main() {
// Set struct
struct ConfigurableOptions<'a> {
name: &'a str,
author: &'a str,
note: &'a str,
data: &'a str,
}
let mut configurable_data = ConfigurableOptions {
name: "",
author: "",
note: "",
data: "",
};
// Set input types
let mut input = String::new();
let mut collect_input: &str;
let mut acumulative_input = String::new();
// Get terminal size
let (width, height) = terminal::size().unwrap();
stdout()
.queue(MoveTo(width / 2 - 45 / 2 + 15, height / 2 - 12))
.unwrap();
stdout().flush().unwrap();
// Set cursor position
let quit = false;
loop {
// Set Key Movement
while !quit {
while poll(Duration::ZERO).unwrap() {
match read().unwrap() {
Event::Key(event) => {
match event.code {
KeyCode::Char(event) => {
input.push(event);
stdout().flush().unwrap();
acumulative_input.push(event);
if acumulative_input.len() == 1 {
stdout().write(input.as_bytes()).unwrap();
} else {
// Put the info in their adequate struct items
let (_cursor_width, cursor_height) = position().unwrap();
if cursor_height == height / 2 - 12 {
// Format last charcater
let last_character: &str = acumulative_input
.chars()
.last()
.unwrap()
.to_string()
.as_str();
// Format collect_input
let collect_input =
collect_input.to_string().push_str(&last_character);
stdout()
.write(last_character.to_string().as_bytes())
.unwrap();
configurable_data.name = collect_input.as_str();
};
}
input = String::new();
}
KeyCode::Home => {
match execute!(
stderr(),
terminal::EnableLineWrap,
terminal::LeaveAlternateScreen,
) {
Err(e) => println!("{:?}", e),
Ok(_) => {}
}
let _ = terminal::disable_raw_mode();
std::process::exit(0);
}
_ => {}
}
}
_ => {}
}
}
}
}
}