#Im new, can i get a start?
76 messages · Page 1 of 1 (latest)
in c# you could do Console.Writeline("")
and
Console.Writeline($"{}")
or
Console.Writeline (@" ")
usually, you use a macro (println! or print!)
print is without \n
right
and println! is like with
what do you mean with "in front of a class". Do you mean the !?
fn main()
i think its two different questions, one about printing and another about function syntax
yep
main is just the name of a function in this case (and main is where binaries start from, similar to c)
print! and println! are macros to allow a variable number of arguments, for formatting
in c its int Main() but idk why its fn in rust
The syntax is different
i might ask "why doesn't C use fn or fun or def to declare functions"
just different syntax, c has ReturnType function_name(ArgType argname) and rust has fn function_name(argname: ArgType) -> ReturnType
yup
the main reason why syntax in rust is what it is is to make it easy to parse, both for compilers and humans
whats the #include/using in rust?
use ...;
use crate::module::Type; or something similar
#include is an entirely different beast than using by the way
it does a lot more
there is no equivalent of it in rust
overall the most important thing for anyone to do when picking up rust is to read the book, just writing bits of code and googling what you think you need is going to lead to torture
@analog wind -book
there is
include! macro
but it's not for usual stuff
btw if i ever make a class file outside the main file, do i have to write use the namespace and the file aswell or only the namespace
#include can do things include! can't. mostly horrible things, but it can
well, #include has no idea of what it inserts to the code
chapter 7 goes over how to lay out files and modules, but a file already acts as a module - you'd basically end up with "use crate::file::Type"
for let's say two files, something.rs and main.rs it would look like this:
pub struct Something {
pub text: String,
}
mod something;
use something::Something;
fn main() {
let my_something = Something { text: "some text".to_string() };
}
struct and not class? odd
well, rust isn't OOP
ah hell naw
there is a thing in rust, called traits, and imo it's actually better than normal inheritance stuff
im listening
They are structs just like structs in C
what about polymorphism?
you do that with traits
we have traits, which are kinda like interfaces in C#
trait Trait2 : Trait1 {}
this syntax is used to define traits that can only be implemented for things that already have the trait on the right implemented
stay calm, it's just the matter of getting used to this
It was already linked here, but I'd strongly recommend you read through the Book, from the start, instead of jumping around to all the unique features of Rust.
rust has a different way of doing things, not worse, just different
rust doesn't have inheritance, but there's a few kinds of polymorphism
and some of that was probably adopted from OCaml and similar
well can you at least tell me rust memory safety, i use pointers sometimes, and when i use references i have no idea if i accidently did it
in rust, references are just pointers that have some compile-time terms and conditions attached
like try and except?
It's also pretty hard to accidentally use pointers. You can create pointers pretty easily, but actually using them would require you to wrap it in the unsafe keyword.
no, i don't see any relation there, references are explained when you get into chapter 4
Like I said, really suggest you read the Book (https://doc.rust-lang.org/book/), it'll give you detailed explanations (in a structured way) of these concepts, without piling all of them on at the same time.
in rust, you can't have a reference that outlives the value
and all "owned" values get destroyed when they get out of scope
and in arrays/lists/vectors?
then you use other stuff for that
same principle, rust uses raii much like c++
if a collection/structure goes out of scope, anything in it that needs to be freed will be, and so on recursively
yes, usually there is no need for manually deallocating things
unless you deal with unsafe code, which is, yk, unsafe
neat
there's a lot of nice features in rust, but it takes some time to know about them, so just try to be patient and don't rush things