Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7582323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:24:28+00:00 2026-05-30T18:24:28+00:00

I’m trying to connect to mysql from rust code. I’ve tried these steps. 1.

  • 0

I’m trying to connect to mysql from rust code. I’ve tried these steps.

    1. I wrote c code using mysql.h, and command below.

    
     $ gcc -shared mysqlrust.c -o libmysqlrust.so  $(mysql_config --cflags) $(mysql_config --libs)   $(mysql_config --cflags)
     $ cp libmysqlrust.so /usr/local/lib/rustc/i686-unknown-linux-gnu/lib/
    

    2. I wrote Rust code, that calls libmysqlrust.so.

But I couldn’t figure out way to use C type structure “MYSQL“, “MYSQL_RES“, “MYSQL_ROW“.
Please show me how to use c type structure from rust code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T18:24:29+00:00Added an answer on May 30, 2026 at 6:24 pm

    There is not yet any way to automatically create Rust type definitions from C structs. In these situations there are a few ways to proceed. Not knowing the MySQL API, I can’t say exactly what you should do, but here are some options.

    1) Treat them entirely as opaque pointers.

    This is the best situation to be in, and depends on the C API always taking the struct as a pointer, having its own constructor and destructor functions, and providing accessor functions for whatever you need to access inside the struct. In these cases you just define type MYSQL = ctypes::void and only ever use it as an unsafe pointer *MYSQL. Sometimes the easiest path is to write your own C wrappers to fill in the gaps and make this scenario possible.

    The remaining scenarios all involve redefining a Rust data structure with the same structure as the C struct. Rust tries to lay out its data structures in a way that is compatible with C (though doesn’t always succeed yet), so it is often possible to create a Rust record or enum with the size, alignment and layout of the C struct you care about. You will want to make sure you use the types in core::ctypes, as they are defined to match various common C types.

    Note that the ctypes module will be going away soon in favor of a more comprehensive libc compatibility module.

    2) Define a Rust record that is partially correct.

    If the API provides constructors and destructors, but you still need access to some fields of the struct, then you can define just enough of the struct to get at the fields you care about, disregarding things like the correct size and alignment. e.g. type MSQL = { filler1: ctypes::int, ..., connector_fd: *ctypes::char }. You can stop defining the struct at the last field you care about since you have a C function to allocate it on the heap with the correct size and alignment. In Rust code you always refer to it with an unsafe pointer: let mysql: *MYSQL = mysqlrust::create_mysql();

    3) Define a Rust record that is the correct size and alignment, without caring about the contents.

    If you don’t have constructor/destructor functions, or need to store the struct on the stack, but you otherwise have accessor functions to manipulate the contents of the struct, then you need to define a Rust record with the correct size and alignment. To do this, just add fields of type uint (which is always pointer-sized) or tuples of uint, until both C’s sizeof and core::sys::size_of agree on the size. Pad with u8s if the size isn’t a multiple of the pointer size. Getting the alignment right is a more mystical process, but by using uint fields you will generally end up with a usable alignment (maybe – I really have no idea how accurate that statement is).

    I would recommend adding tests to sanity check that Rust and C agree on the size in order to guard against future breakage.

    3) Actually redefine the entire C struct.

    This is a pretty dire situation for large structs, and it is possible in theory, but I don’t think anybody has done it for a struct as big as MYSQL. I would avoid it if you can. Eventually there will be a clang-based tool to do this automatically.

    Here are some examples of interop with C structs:

    https://github.com/jdm/rust-socket/blob/master/socket.rs – This redefines various socket structs, adding placeholders for fields it doesn’t care about. Note that it uses u8 for padding, but I think uint is more likely to produce correct alignment.

    https://github.com/erickt/rust-zmq/blob/master/zmq.rs

    https://github.com/pcwalton/rust-spidermonkey – This one demonstrates interop with a somewhat complex API.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.