You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
This Rust library provides a convenient way to interact with the LeetCode API, allowing you to programmatically access LeetCode problems, submit solutions, and retrieve submission results.
Features
Retrieve a list of LeetCode problems.
Fetch problem details, including the problem description, constraints, and examples.
Submit solutions to LeetCode problems.
Check submission results, including status, runtime, and memory usage.
Installation
Add the following line to yourCargo.toml file:
[dependencies]leetcoderustapi ="1.0.8"
Usage
Authentication
To use the LeetCode API, you need to obtain an authentication token. Follow the instructions provided by LeetCode to obtain your token.
Example: Action with problems
use leetcoderustapi::{problem_build::{Tags,Category,Difficulty,Status},UserApi,ProgrammingLanguage,};#[tokio::main]asyncfnmain(){// Set cookie from leetcodelet token = std::env::var("COOKIE").expect("cookie doesn't set");// Create a new LeetCode clientlet api =UserApi::new(&token).await.unwrap();// Show found problems by keyword and show 5 noteslet show_problems = api.show_problm_list("sum",5).await.unwrap();// Find problems by properties with creating problem builderlet problems_builder = api.problem_builder().set_category(Category::Algorithms).set_difficulty(Difficulty::Easy).set_keyword("sum")//max show notes limit is 2763; default is 5.set_note_limit(3).set_status(Status::Solved)//max tags over 50+.set_tags(vec![Tags::Array,Tags::BinarySearch,]).build().await.unwrap();// Fetch the full data for a specific problem by it's namelet problem_info = api.set_problem("two sum").await.unwrap();// Fetch the full data for a specific problem by it's IDlet problem_info = api.set_problem_by_id(1).await.unwrap();// Retrieve previous submissions to this problemlet my_submissions = problem_info.my_submissions().await.unwrap();// Retrieve code snippetslet code_snippets = problem_info.code_snippets().unwrap();// Retrieve solution infolet solution_info = problem_info.solution_info().unwrap();// Retrieve related topicslet related_topics = problem_info.related_topics();// Retrieve similar questionslet similar_questions = problem_info.similar_questions().unwrap();// Retrieve statslet stats = problem_info.stats().unwrap();// Retrieve hintslet hints = problem_info.hints();// Retrieve descriptionlet description = problem_info.description().unwrap();// Retrieve difficultylet difficulty = problem_info.difficulty();// Retrieve likes and dislikeslet likes = problem_info.rating().unwrap();// Retrieve categorylet category = problem_info.category();// We also can send submissions and tests// Need to specify a lang and provided codelet subm_response = problem_info.send_subm(ProgrammingLanguage::Rust,"impl Solution { fn two_sum() {}}").await.unwrap();let test_response = problem_info.send_test(ProgrammingLanguage::Rust,"impl Solution { fn two_sum() {}}").await.unwrap();}
Example: Actions with Self profile
#[tokio::main]asyncfnmain(){// Set cookie from leetcodelet token = std::env::var("COOKIE").expect("cookie doesn't set");// Create a new LeetCode clientlet api =UserApi::new(&token).await.unwrap();// Create interaction with profilelet user_profile = api.my_profile().await.unwrap();// Create empty list of the problems with provided name user_profile.create_list("my_new_favorite_list").await.unwrap();// Rename list user_profile.rename_list("my_new_favorite_list","hard_problems").await.unwrap();// Set list puplic user_profile.set_public("hard_problems").await.unwrap();// Set list private user_profile.set_private("hard_problems").await.unwrap();// Get link to the list if it is a publiclet share_list_url = user_profile.get_share_url("hard_problems").await.unwrap();// Show existing listslet lists = user_profile.show_lists();// Delete list with provided name user_profile.delete_list("hard_problems").await.unwrap();// Show users last 10 notificationlet notifications = user_profile.get_notifications().await.unwrap();// Deactivate token(logout) user_profile.deactivate_token().await.unwrap();}
Example: Actions with Public user profile
#[tokio::main]asyncfnmain(){// Set cookie from leetcodelet token = std::env::var("COOKIE").expect("cookie doesn't set");// Create a new LeetCode clientlet api =UserApi::new(&token).await.unwrap();// Find public userlet user = api.find_profile("1101-1").await;// Check public user common statslet user_stats = user.user_stats().await.unwrap();// Check what langs used userlet lang_stats = user.language_stats().await.unwrap();// Check what problems (by tags) solve userlet skill_stats = user.skill_stats().await.unwrap();// Show rating by beating problemslet beat_stats = ser.problem_beat_stats().await.unwrap();// Show recent submissons of userlet beat_stats = ser.recent_subm_list().await.unwrap();}
Important
Replace"COOKIE" with your actual LeetCode authentication cookie.