Home > Technology peripherals > AI > body text

With GPT+Copilot, learning Rust can take off directly

WBOY
Release: 2024-01-11 19:27:18
forward
897 people have browsed it

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.

有了GPT+Copilot,学习 Rust 直接起飞

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
Copy after login

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(())}
Copy after login

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"])% 
Copy after login

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(())}
Copy after login

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.

有了GPT+Copilot,学习 Rust 直接起飞

The running results are as follows:

有了GPT+Copilot,学习 Rust 直接起飞

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!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!