Home > Article > Technology peripherals > With GPT+Copilot, learning Rust can take off directly
Hello everyone, I am a fisherman.
Rust has a slightly steep learning curve in the early and mid-term, but now with AI assistance, learning programming languages is no longer that difficult. By asking questions using GPT, you can solve problems quickly.
For example, if I want to read a CSV file, I can directly go to GPT for consultation.
It even generates the content in the csv for you, which is so efficient.
First, let's add external packages: use the csv and serde packages. These packages can be imported by adding them in your Cargo.toml file.
You can also choose to add the command line:
cargo add serde --features derivecargo add csvcargo add serde --features derive
After adding, we try to traverse the file to see if the data actually exists.
The code is as follows:
use csv::Reader;use std::error::Error;const CSV_PATH: &str = "./large_file.csv";fn main() -> Result<(), Box<dyn Error>> {let mut rdr = Reader::from_path(CSV_PATH)?;for result in rdr.records() {let record = result?;println!("{:?}", record);}Ok(())}
The running results really help generate these data.
cargo runFinished dev [unoptimized + debuginfo] target(s) in 0.04s Running `target/debug/rust-demo9`StringRecord(["Alice", "30", "New York"])StringRecord(["Bob", "25", "Los Angeles"])StringRecord(["Charlie", "35", "Chicago"])StringRecord(["David", "40", "Houston"])StringRecord(["Eve", "28", "Philadelphia"])StringRecord(["Frank", "33", "Phoenix"])StringRecord(["Grace", "22", "San Antonio"])StringRecord(["Henry", "45", "San Diego"])StringRecord(["Ivy", "29", "Dallas"])StringRecord(["Jake", "38", "San Jose"])%
If I want to read only the data in the Age column, I can go directly to GPT for consultation. He can help you solve it. Even every line of the code can be explained clearly to you. This is help for beginners. It must be big.
use csv::Reader;use std::error::Error;const CSV_PATH: &str = "./large_file.csv";fn main() -> Result<(), Box<dyn Error>> {let mut rdr = Reader::from_path(CSV_PATH)?;for result in rdr.records() {let record = result?;println!("{}", record.get(1).unwrap_or_default());}Ok(())}
If I want to get the third column of data, I can also directly consult GitHub Copilot in vscode. If there is no problem, I can directly click Accept to quote the code it provides you. It is also very convenient to modify the basics, but Some basic codes and repeated codes can be handed over to Copilot to automatically generate them for you, which also improves efficiency a lot.
The running results are as follows:
Finally, learning any language has reduced a lot of difficulty, even You can use AI as your product manager to provide you with requirements, and finally let AI help you develop code. You are actually the Reviewer. If you write poorly, you can continue to let AI modify it or you can basically use it at work. You don't need to learn most of the repetitive code from scratch, but you need to learn to use tools.
The above is the detailed content of With GPT+Copilot, learning Rust can take off directly. For more information, please follow other related articles on the PHP Chinese website!